Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

wpf - Bind command from Business object to View in MVVM

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...