KeyDown事件被触发两次

时间:2018-02-21 12:19:44

标签: c# wpf xaml

我有一个包含两个TextBox的样式,以便创建PlaceHolder / Watermark,到目前为止工作正常。
唯一的例外是事件被触发两次,第一个来自{{1我有CustomTextBox,第二个来自我Style中的CustomTextBox

有没有办法阻止这种行为?我已经尝试设置XAMLIsEnable="False",但似乎无法正常工作。

这里我用来模拟水印的风格:

ReadOnly="True"

任何帮助将不胜感激。提前谢谢。

编辑1:事件处理的<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> 类的代码

CustomTextBox

编辑2: 在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); } } } 上使用CustomTextBox

UserControl

2 个答案:

答案 0 :(得分:0)

根据您的评论,首先它来自&#34;测试源&#34;然后转到CustomTextBox表示在第一个用户控件上收到KeyDown事件,然后传递给第二个用户控件。

您能否确保将Operations to perform: Apply all migrations: delta_device, admin, menu, sessions, datapipeline, datacollector, siemens_s7, contenttypes, auth, settings Running migrations: Rendering model states... DONE Applying delta_device.0002_auto_20171210_1631...Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 350, in execute_from_command_line utility.execute() File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 342, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 348, in run_from_argv self.execute(*args, **cmd_options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 399, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 200, in handle executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 92, in migrate self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 198, in apply_migration state = migration.apply(state, schema_editor) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 123, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards schema_editor.alter_field(from_model, from_field, to_field) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 482, in alter_field old_db_params, new_db_params, strict) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 565, in _alter_field new_default = self.effective_default(new_field) File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 210, in effective_default default = field.get_db_prep_save(default, self.connection) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 728, in get_db_prep_save prepared=False) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 720, in get_db_prep_value value = self.get_prep_value(value) File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/__init__.py", line 1853, in get_prep_value return int(value) ValueError: invalid literal for int() with base 10: '0x000 放入&#34;测试源&#34;通知它是在第一个用户控件上处理的?

答案 1 :(得分:-1)

&#34;固定&#34;在评论中过滤{@ 1}}作为@Amol Bavannavar的消息:

e.Source