【发布时间】:2019-07-15 13:16:32
【问题描述】:
我有一个DataGrid 和两个按钮。按第一个按钮将调用一个命令,该命令使用DataTable 从数据库表中填充我的DataGrid。第二个按钮将应用一些逻辑并最终从数据库表中删除数据。我的问题是,在我选择第二个按钮后,DataGrid 没有更新,仍然显示数据库中不存在的行。这是我的设置:
Button1(填充 DataGrid):
<Button Content="Load Data" Command="{Binding FillMyDataTableCommand}"/>
数据网格:
<DataGrid ItemsSource="{Binding MyDataTable}">
Button2(应用逻辑,数据不应该再出现在表中):
<Button Command="{Binding StartCommand}" CommandParameter="{Binding ElementName=myDataGrid}"</Button>
在选择 button2 时,我的 ViewModel 负责逻辑,在逻辑之后我想更新数据网格以显示对表的更改:
myDataGrid.Items.Refresh();
我不能在 ViewModel 中执行此操作,因为这会违反 MVVM,因为它应该对视图一无所知。我尝试了一种解决方法,方法是使用单击命令添加按钮背后的代码,希望它会在绑定命令之后执行。但是它首先执行:
Button2(尝试):
<Button Command="{Binding StartCommand}" CommandParameter="{Binding ElementName=myDataGrid}" click="button_click_Refresh"</Button>
我该如何解决这个问题?
【问题讨论】: