我有一个自定义元素,如下所示:
<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。我还没有找到一种方法如何将双击事件的焦点移动到文本框。