【发布时间】:2017-02-12 08:26:41
【问题描述】:
我有一个datagrid;Printreport 通过observable collection 填充; PopulatePatternData
它运行良好,并且在程序运行时显示所有行。
当程序重新运行时,我想用新的数据行更新数据网格,而是用以前的结果(行)添加新数据
XAML:
<DataGrid x:Name="PrintReport" ItemsSource="{Binding PopulatePatternData}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="12" CanUserSortColumns="False"
HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AlternatingRowBackground="Gainsboro" AlternationCount="1"
SelectionMode="Extended" SelectionUnit="Cell" >
我用来运行程序的按钮 (RunAnalysis) 有一个event handler。我在单击 observable collection 时清除它,然后创建数据网格。
我尝试“clear”如下所示的数据网格行,但无济于事。
private void RunAnalysis(object sender, RoutedEventArgs e)
{
WFPlanningCompliancePluginModel model = DataContext as WFPlanningCompliancePluginModel;
model.PopulatePatternData.Clear();
PrintReport.Items.Clear(); // This does not work
model.Run();
}
collection 被清除,但datagrid 没有被清除!我错了什么?
如果我使用,程序第一次运行时会失败
PrintReport.Items.Clear(); as it does not find any items.
【问题讨论】:
-
如果绑定正常,就不需要 PrintReport.Items.Clear(); - 你能展示模型的代码吗?
-
如果您正在清除数据库表,那么您需要再次刷新您的数据网格。