Event handler in DataTemplate
Asked Answered
M

4

11

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?

Mickimickie answered 25/11, 2009 at 23:13 Comment(0)
M
4

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);
            }
        }
    }
} 
Mickimickie answered 27/11, 2009 at 0:52 Comment(0)
H
15

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>
Hesson answered 27/5, 2012 at 16:13 Comment(0)
M
4

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);
            }
        }
    }
} 
Mickimickie answered 27/11, 2009 at 0:52 Comment(0)
S
0

This article has a way to route any Event to Command

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

Supercharge answered 17/1, 2014 at 15:59 Comment(0)
C
0

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;
    }
Concertante answered 14/6, 2023 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.