[英]WPF Datagrid rows do not get cleared - C#
I have a datagrid
; 我有一个
datagrid
; Printreport
populated via an observable collection
; 通过
observable collection
填充Printreport
; PopulatePatternData
It runs fine and all rows are displayed when the program is run. PopulatePatternData
运行正常,并且在运行程序时显示所有行。 When the program is rerun, I would like to get the datagrid updated with the new data rows but instead new data gets added with the previous results (rows) 当程序重新运行时,我想用新的数据行更新数据网格,但是将新数据与以前的结果(行)一起添加
XAML: 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" >
The button (RunAnalysis) that I use to run the program has an event handler
. 我用来运行程序的按钮(RunAnalysis)具有
event handler
。 I clear the observable collection
when it is clicked and then the datagrid is created. 当单击
observable collection
然后创建数据网格时,我将其清除。 I tried to " clear
" the datagrid rows as shown below but to no avail. 我试图如下所示
clear
“ datagrid”行,但无济于事。
private void RunAnalysis(object sender, RoutedEventArgs e)
{
WFPlanningCompliancePluginModel model = DataContext as WFPlanningCompliancePluginModel;
model.PopulatePatternData.Clear();
PrintReport.Items.Clear(); // This does not work
model.Run();
}
The collection
gets cleared but not the datagrid
! collection
将被清除,但不会清除datagrid
! what am i dong wrong? 我在做什么错? The program fails when it is run the first time if I use
如果我使用该程序,则在第一次运行时会失败
PrintReport.Items.Clear(); as it does not find any items.
Rebind the DataGrid
after clearing the items: 清除项目后,重新
DataGrid
:
private void RunAnalysis(object sender, RoutedEventArgs e)
{
WFPlanningCompliancePluginModel model = DataContext as WFPlanningCompliancePluginModel;
model.PopulatePatternData.Clear();
// PrintReport.Items.Clear(); // This you can skip if the binding works.
PrintReport.DataBind(); // This should be enough if the binding works correctly!
model.Run();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.