KeyDown 事件被触发两次 [英] KeyDown Event is triggered twice
问题描述
我有一个样式,其中包含两个文本框以创建占位符/水印,到目前为止工作正常.
唯一的例外是事件被触发两次,第一个来自 CustomTextBox
我在 Style
中,第二个来自 CustomTextBox
我在 XAML
中有.
是有什么办法可以防止这种行为?我已经尝试设置 IsEnable="False"
和 ReadOnly="True"
但似乎不起作用.
这里是我使用的样式模拟水印:
I have a style which includes two TextBoxes in order to create a PlaceHolder / Watermark, so far is working fine.
The only exception is that the events are triggered twice, the first one comes from the CustomTextBox
I have in the Style
, and the second one from the CustomTextBox
I have in XAML
.
Is there any way to prevent this behaviour? I have already tried to set IsEnable="False"
and ReadOnly="True"
but doesn't seems to work.
Here the style I use to simulate the Watermark:
<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>
任何帮助将不胜感激.提前致谢.
Any help would be appreciated. Thanks in advance.
编辑 1:在 CustomTextBox
类上编写用于事件处理的代码
EDIT 1: Code on the CustomTextBox
Class for the Event handling
protected override void OnKeyDown(KeyEventArgs e)
{
//Filtering "solution"
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);
}
}
}
编辑 2:在我的 UserControl
上使用 CustomTextBox
:
EDIT 2:
Use of the CustomTextBox
on my UserControl
:
<Utils:CustomTextBox Grid.Row="0"
Margin="0,10"
KeyboardViewModel="{Binding Path=MainWindowViewModel.KeyboardViewModel}"
HintText="Patient"
x:Name="Patient"/>
推荐答案
根据您的评论,首先它来自测试源",然后转到 CustomTextBox 意味着 KeyDown 事件在第一个用户控件上收到然后传递到第二个用户控件.
Based on your comment, first it comes from the "test source" and then goes to CustomTextBox means that the KeyDown event is received on first user control and then passed to the second user control.
您能否确保将 e.Handled = true
放在测试源"中以通知它是在第一个用户控件上处理的?
Can you make sure that you put e.Handled = true
in the "test source" to notify that it is handled on the first user control?
这篇关于KeyDown 事件被触发两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!