绑定命令从业务对象到MVVM中查看 [英] Bind command from Business object to View in MVVM

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

问题描述

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

 < DataGrid CanUserAddRows =TrueItemsSource ={Binding Path = PersonsInfo}AutoGenerateColumns =False
CanUserDeleteRows =TrueCanUserReorderColumns =True
CanUserSortColumns =True>
< DataGrid.Columns>
< DataGridTextColumn Header =NameBinding ={Binding Path = Name}/>
< DataGridTextColumn Header =AgeBinding ={Binding Path = Age}/>
< DataGridTextColumn Header =出生日期Binding ={Binding Path = DateOfBirth}/>
< DataGridTextColumn Header =AddressBinding ={Binding Path = Address}/>
< DataGridTemplateColumn>
< DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< Grid>
< Button Content =Remove ...Margin =3Command ={Binding Path = RemoveCommand}/>
< / Grid>
< / DataTemplate>
< /DataGridTemplateColumn.CellTemplate>
< / DataGridTemplateColumn>
< /DataGrid.Columns>
< / DataGrid>

在上述代码中,当我点击按钮时,我需要从DataGrid中删除记录。 / p>

所以我需要的是,我应该在业务对象类中使用Command,而不是在ViewModel类中。



当我点击每行中的按钮时,应该删除相应的行。



因此,如何找到在DataGrid中选择哪个项目通过业务对象类中的命令执行删除行,因为业务对象类没有关于DataGrid项目的信息?

解决方案

总而言之,不要将你的命令放在你的模型中,而是通过 RelativeSource 使用绑定。像这样:

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

其次,您可以绑定 DataGrid code> SelectedItem 到您的财产 ViewModel

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

或通过 CommandParameter 传递所选项目。

 < Button Command ={Binding RelativeSource = {RelativeSource AncestorType = {x:Type DataGrid}},Path = DataContext。 RemoveCommandCommandParameter ={Binding}/> 


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>

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

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.

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?

解决方案

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}" />

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

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

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天全站免登陆