将业务对象的命令绑定到 MVVM 中的视图 [英] Bind command from Business object to View in MVVM

查看:23
本文介绍了将业务对象的命令绑定到 MVVM 中的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 MVVM 在 WPF 中填充 DataGrid.我有具有 4 个属性的业务对象来在 DataGrid 中创建行和列.

I populate DataGrid in WPF through MVVM. I have business object with 4 properties to create the Row and Columns in the DataGrid.

<DataGrid CanUserAddRows="True" ItemsSource="{Binding Path=PersonsInfo}" AutoGenerateColumns="False"
                  CanUserDeleteRows="True" CanUserReorderColumns="True" 
                  CanUserSortColumns="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/>
                <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
                <DataGridTextColumn Header="Date Of Birth" Binding="{Binding Path=DateOfBirth}"/>
                <DataGridTextColumn Header="Address" Binding="{Binding Path=Address}"/>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <Button Content="Remove..." Margin="3" Command="{Binding Path=RemoveCommand}" />
                            </Grid>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

在上面的代码中,当我单击按钮时,我需要从 DataGrid 中删除记录.

In the above code when I click the button, I need to remove the records from the DataGrid.

所以我需要的要求是,我应该在业务对象类中拥有 Command,而不是在 ViewModel 类中.

So I need the requirement that, I should be having the Command in the business object class instead of having inside the ViewModel class.

当我点击每一行的按钮时,相应的行应该被删除.

While I am clicking the button in each row, that corresponding row should be deleted.

由于业务对象类没有DataGrid的项目信息,所以如何通过业务对象类中的命令执行找到DataGrid中的哪个项目被选中删除行?

Hence how can I find which item is selected in the DataGrid to delete the row through command execution in business object class because business object class does not have information about items of the DataGrid?

推荐答案

首先,不要把你的命令放到你的 Model 中,而是通过 RelativeSource 使用绑定.像这样:

First of all, do not place your command into your Model, instead use binding through RelativeSource. Like this:

<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.RemoveCommand}" />

其次,您可以将您的 DataGrid SelectedItem 绑定到您的 ViewModel

Second, you can bind your DataGrid SelectedItem to property of you ViewModel

<DataGrid SelectedItem="{Binding SelectedItemProperty, Mode=TwoWay}" .../>

或通过 CommandParameter 传递您选择的项目.

or pass your selected item through CommandParameter.

<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.RemoveCommand}" CommandParameter="{Binding}" />

这篇关于将业务对象的命令绑定到 MVVM 中的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆