DataTemplate 中的事件处理程序

发布于 2024-08-12 09:53:11 字数 125 浏览 5 评论 0原文

我在数据模板中有 WPF ComboBox(列表框中有很多组合框),我想处理输入按钮。如果它是一个按钮,那就很容易了 - 我会使用命令+相对绑定路径等。不幸的是,我不知道如何使用命令处理按键或如何从模板设置事件处理程序。 有什么建议吗?

I have WPF ComboBox inside a data template (a lot of comboboxes in listbox) and I want to handle enter button. It would be easy if it was e.g. a button - I would use Command + Relative binding path etc. Unfortunately, I have no idea how handle key press with a Command or how to set event handler from template.
Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放飞的风筝 2024-08-19 09:53:11

您可以按照设置模板的样式使用 EventSetter:

<Style TargetType="{x:Type ListBoxItem}">
      <EventSetter Event="MouseWheel" Handler="GroupListBox_MouseWheel" />
      <Setter Property="Template" ... />
</Style>

You can use the EventSetter in the style you are setting the template with:

<Style TargetType="{x:Type ListBoxItem}">
      <EventSetter Event="MouseWheel" Handler="GroupListBox_MouseWheel" />
      <Setter Property="Template" ... />
</Style>
一曲琵琶半遮面シ 2024-08-19 09:53:11

我通过使用常用的事件处理程序解决了我的问题,在该处理程序中,我遍历可视化树,找到相应的按钮并调用它的命令。
如果其他人也有同样的问题,请发表评论,我将提供更多实现细节。

UPD

这是我的解决方案:

我在可视化树中搜索按钮,然后执行与按钮关联的命令。

View.xaml:

<ComboBox KeyDown="ComboBox_KeyDown"/>
<Button Command="{Binding AddResourceCommand}"/>

View.xaml.cs:

private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        var parent = VisualTreeHelper.GetParent((DependencyObject)sender);
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i) as Button;
            if (null != child)
            {
                child.Command.Execute(null);
            }
        }
    }
} 

I've solved my problem by using a usual event handler where I walk through the visual tree, find corresponding button and call it's command.
If anybody else has the same problem, please post a comment and I'll provide more details of realization.

UPD

Here is my solution:

I search the visual tree for a button and than execute command associated with button.

View.xaml:

<ComboBox KeyDown="ComboBox_KeyDown"/>
<Button Command="{Binding AddResourceCommand}"/>

View.xaml.cs:

private void ComboBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        var parent = VisualTreeHelper.GetParent((DependencyObject)sender);
        int childrenCount = VisualTreeHelper.GetChildrenCount(parent);

        for (int i = 0; i < childrenCount; i++)
        {
            var child = VisualTreeHelper.GetChild(parent, i) as Button;
            if (null != child)
            {
                child.Command.Execute(null);
            }
        }
    }
} 
○愚か者の日 2024-08-19 09:53:11

本文提供了一种将任何 Event 路由到 Command

http://nerobrain.blogspot.nl/2012/01/wpf-events-to-command.html

This article has a way to route any Event to Command

http://nerobrain.blogspot.nl/2012/01/wpf-events-to-command.html

乱了心跳 2024-08-19 09:53:11

另一个简单的选择是像这样派生控件

  public class MyTextbox : TextBox
  {
    private void OnKeyDown(object sender, KeyEventArgs e)
    {
      e.Handled = true;
      //...
      return;
    }

    private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
      //E.g. delete the content when focused
      e.Handled = true;
      this.Text = null;
      return;
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();
      this.GotKeyboardFocus += OnGotKeyboardFocus;
      this.KeyDown += OnKeyDown;
    }
  }

如果您只是希望控件始终像这样运行,而不是在 OnApplyTemplate() 中添加事件,您当然可以直接重写虚拟方法,例如

    protected override void OnGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
      e.Handled = true;
      this.Text = null;
      return;
    }

Another simple option is to derive the control like this

  public class MyTextbox : TextBox
  {
    private void OnKeyDown(object sender, KeyEventArgs e)
    {
      e.Handled = true;
      //...
      return;
    }

    private void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
      //E.g. delete the content when focused
      e.Handled = true;
      this.Text = null;
      return;
    }

    public override void OnApplyTemplate()
    {
      base.OnApplyTemplate();
      this.GotKeyboardFocus += OnGotKeyboardFocus;
      this.KeyDown += OnKeyDown;
    }
  }

If you just want the control to behave always like this, instead of adding the events in OnApplyTemplate() you can of course directly override the virtual methods e.g.

    protected override void OnGotKeyboardFocus(System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
      e.Handled = true;
      this.Text = null;
      return;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文