Button inside a WPF List view/data grid

こ雲淡風輕ζ 提交于 2019-12-04 12:18:31

Three possible solutions that you can choose.

  • Pass current row object as a CommandParameter. In this case, you will modify OnRun method a little. (recommended)

<Button Content="Address" Command="{Binding Path=DataContext.RunCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}" CommandParameter={Binding} />

I think CommandParameter={Binding} works fine because the DataContext of each row is each Customer object.

and OnRun method needs to be modified in order to get a parameter as an argument.


    private void OnRun(object o){
        if(!(o is Customer)) return;
        // Do something
    }

  • Or, Write a little code-behind with SelectionChanged event handling. (not recommended)

  • Or, Use EventToCommand in MVVM-light toolkit. (not recommended)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!