我有一个自定义元素,如下所示:
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Grid>
<TextBox Name="textBox"
Grid.ZIndex="1"
Padding="0,3,0,0"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type Label}}, Path=Content, UpdateSourceTrigger=PropertyChanged}"
Opacity="0"
IsEnabled="False"
Focusable="True"
/>
<Border Name="boxBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1" Background="{TemplateBinding Background}" Padding="3,0,0,0" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
</Grid>我想要一个标签,你可以双击,然后变成一个输入字段(文本框)。因此,我使用以下命令定义了一个双击事件:
<EventTrigger RoutedEvent="MouseDoubleClick">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="textBox"
Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
<BooleanAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="textBox"
Storyboard.TargetProperty="IsEnabled">
<DiscreteBooleanKeyFrame Value="True" KeyTime="0" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>双击可以很好地工作,但您必须执行三次单击才能将焦点置于文本框中。双击只会将不透明度设置为1。我还没有找到一种方法来将双击事件的焦点移动到文本框中。
发布于 2012-10-30 06:19:36
我觉得this能帮上忙。它包含一些代码,但它是一个使用WPF行为的可重用和可伸缩的解决方案。
发布于 2012-10-30 11:45:31
您可以实现文本框的附加行为,当文本框处于启用或可见状态时,该行为将焦点设置在文本框上。
https://stackoverflow.com/questions/13130273
复制相似问题