如何使用手势更改自定义控件的内容?

时间:2014-12-21 20:06:06

标签: wpf xaml custom-controls inkcanvas

我迷路了...回到SO :)

以下代码通过我的customcontrol,ccGistaFigure正确显示文本行。我现在将InkCanvas放在控件上。最初,InkCanvas将设置为仅识别手势。

我想弄清楚的是,在接收到特定手势时,InkCanvas可以告诉自定义控件自行更改,还是有办法让XAML将ccGistaFigure替换为另一个ccGistaFigure对象?

具体来说,如果我点击InkCanvas上的一个单词,我希望能够确定我点击的单词并强制customcontrol在该单词的字母之间添加空格。因此,如果单词是" Testing",则单词上的点击(或手势)应该将文本更改为" T_e_s_t_i_n_g"。

(ccGistaFigure派生自FrameworkElement并直接从FormattedText对象显示字符串。)

假设我可以确定被点击的单词,更改显示的最佳方法是什么?

我目前的XAML是:

<UserControl x:Class="Nova5.UI.Views.Ink.InkEditorView"
       .....

<Grid Background="#FFE24848" >

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
        <Canvas Grid.Row="1" Grid.RowSpan="3">

        <ScrollViewer VerticalScrollBarVisibility="Auto" 
            Width="{Binding Parent.ActualWidth, Mode=OneWay, RelativeSource={RelativeSource Self}}"
            Height="{Binding Parent.ActualHeight, Mode=OneWay, RelativeSource={RelativeSource Self}}" 
              >
           <Grid Background="White" >
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition Height="100"/>
                </Grid.RowDefinitions>
                <wc:ccGistaFigure Grid.Row="0"
                                  Text="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" 
                                  LineHeight="{Binding LineHeight, UpdateSourceTrigger=PropertyChanged,Mode=OneWayToSource}"
                                  AscenderlineOffset="{Binding AscenderlineOffset, UpdateSourceTrigger=PropertyChanged,Mode=OneWayToSource}"
                                  DescenderlineOffset="{Binding DescenderlineOffset, UpdateSourceTrigger=PropertyChanged,Mode=OneWayToSource}"
                                  MidlineOffset="{Binding MidlineOffset,UpdateSourceTrigger=PropertyChanged,Mode=OneWayToSource}"
                                  BaselineOffset="{Binding BaselineOffset, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
                                  WritingPadSize="{Binding WritingPadSize, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}"
                                  />


                <InkCanvas  Grid.Row="0" Grid.RowSpan="2"
                    Background="Transparent" 
                    DefaultDrawingAttributes="{Binding Pen}" 
                    EditingMode="{Binding EditingMode}" 
                    Strokes="{Binding Strokes}" />

            </Grid>
        </ScrollViewer>
    </Canvas>

</Grid>
</UserControl>

任何想法都非常感谢。感谢。

1 个答案:

答案 0 :(得分:1)

如果您想在ccGistaFigure上启用触控事件,则必须这样做:

<wc:ccGistaFigure 
     x:Name="gistaFigure"
     TouchDown="TouchEventHandler"
     IsManipulationEnabled="true" ../>

并在代码隐藏中定义:

private void TouchEventHandler(object sender, TouchEventArgs e)
{
    gistaFigure.InternalText = string.Join("_", gistaFigure.Text.ToCharArray())
}