开发者

WPF Can a button carry data to it's event handler

Ok this might be simple or just not possible. Probably it's something that is frowned upon.

Let's say I have a combobox and a button beside it. I choose a item from the combobox and press the button.

In my button handler I guess I'm supposed to fetch combobox.selectedValue to get the item ( is there a better way).

Let's say I don't have access to the combobox where the eventhandler is could I bind the button somehow to the selectedvalue so I would have access to it through the button?

    private void addItemClick(ob开发者_C百科ject sender, RoutedEventArgs e)
    {
          collection.add( classFactory.NewInstanceOfId( (sender as button).DataContainer);
    }

Is this something you shouldn't do? Does Combobox perhaps have some inherit event to do this (I want a button to confirm). If I include the button inside the ComboBox somehow I guess I could use Button.DataContext ?


You can bind the clicked to a command and have the command parameter set to the selected value.

Have a look here (although not exactly what you want but illustrates how to do it):

Passing two command parameters using a WPF binding

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜