C# KeyDown事件被触发两次
我有一个包含两个文本框的样式,用于创建占位符/水印,目前为止工作正常。C# KeyDown事件被触发两次,c#,wpf,xaml,C#,Wpf,Xaml,我有一个包含两个文本框的样式,用于创建占位符/水印,目前为止工作正常。唯一的例外是事件被触发两次,第一次来自我在样式中的自定义文本框,还有我在XAML中的CustomTextBox中的第二个问题,有什么方法可以防止这种行为吗?我已经尝试设置IsEnable=“False”和ReadOnly=“True”,但似乎不起作用。这里是我用来模拟水印的样式: <Style x:Key="CustomTextBoxStyle" TargetType="{x:Type utils:Cust
唯一的例外是事件被触发两次,第一次来自我在
样式中的自定义文本框,还有我在XAML
中的CustomTextBox
中的第二个问题,有什么方法可以防止这种行为吗?我已经尝试设置IsEnable=“False”
和ReadOnly=“True”
,但似乎不起作用。
这里是我用来模拟水印的样式:
<Style x:Key="CustomTextBoxStyle"
TargetType="{x:Type utils:CustomTextBox}">
<Setter Property="FontFamily"
Value="/UserInterface;component/Resources/Fonts/#Avenir LT Std 35 Light" />
<Setter Property="FontSize"
Value="16" />
<Setter Property="Foreground"
Value="#FF414042" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type utils:CustomTextBox}">
<Border Name="Border"
BorderBrush="#FF348781"
BorderThickness="0,0,0,4"
CornerRadius="2">
<ScrollViewer x:Name="PART_ContentHost"
Margin="0" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="Disabled" />
<VisualState x:Name="ReadOnly" />
<VisualState x:Name="MouseOver" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type utils:CustomTextBox}"
BasedOn="{StaticResource CustomTextBoxStyle}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type utils:CustomTextBox}">
<Grid>
<utils:CustomTextBox
Text="{TemplateBinding Text}"
x:Name="textSource"
Background="Transparent"
Panel.ZIndex="2"
Style="{StaticResource CustomTextBoxStyle}"
KeyboardViewModel="{TemplateBinding KeyboardViewModel}"/>
<utils:CustomTextBox Text="{TemplateBinding HintText}"
Background="{TemplateBinding Background}"
Panel.ZIndex="1"
IsEnabled="False">
<utils:CustomTextBox.Style>
<Style TargetType="{x:Type utils:CustomTextBox}"
BasedOn="{StaticResource CustomTextBoxStyle}">
<Setter Property="Foreground"
Value="Transparent" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text, Source={x:Reference textSource}}"
Value="">
<Setter Property="Foreground"
Value="Gray" />
<Setter Property="HorizontalContentAlignment"
Value="Left" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
</DataTrigger>
</Style.Triggers>
</Style>
</utils:CustomTextBox.Style>
</utils:CustomTextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
编辑2:
在myUserControl上使用CustomTextBox
:
<Utils:CustomTextBox Grid.Row="0"
Margin="0,10"
KeyboardViewModel="{Binding Path=MainWindowViewModel.KeyboardViewModel}"
HintText="Patient"
x:Name="Patient"/>
根据您的评论,首先它来自“测试源”,然后转到CustomTextBox,这意味着在第一个用户控件上接收按键事件,然后传递给第二个用户控件
您能否确保将e.Handled=true
放在“测试源”中,以通知它是在第一个用户控件上处理的?将e.source
作为@Amol Bavannavar在注释上的建议进行过滤的“修复”:
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Source is CustomTextBox sourceTextBox && sourceTextBox.Name.Equals("textSource"))
{
return;
}
base.OnKeyDown(e);
if (e.Key == Key.Enter && EnterKeyCommand != null)
{
if (EnterKeyCommand.CanExecute(null))
{
EnterKeyCommand.Execute(null);
}
}
}
您是否在自定义控件的代码隐藏中处理事件?显示代码隐藏?使用CustomTextBox中的代码隐藏进行更新。您是否尝试过设置IsReadOnly=True???谢谢@Amolbvannavar,只是测试一下,仍然不起作用。当然,这是建议之一,我可以检查e.Source=“testSource”
但这似乎不是正确的方法,基本上,我想了解为什么事件是从样式中的CustomTextBox触发的。这是否令人满意地回答了您自己的问题?如果是,你可以接受。我不能接受我自己的回答,我必须等两天。@Nekeniehl啊,谢谢你提醒我这条规则。我问的问题不多,我已经忘记了这一点。
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Source is CustomTextBox sourceTextBox && sourceTextBox.Name.Equals("textSource"))
{
return;
}
base.OnKeyDown(e);
if (e.Key == Key.Enter && EnterKeyCommand != null)
{
if (EnterKeyCommand.CanExecute(null))
{
EnterKeyCommand.Execute(null);
}
}
}