【发布时间】:2017-11-28 13:04:44
【问题描述】:
我的数据网格有问题。如果我在源类中挂起一个属性,它不会更新。
这就是在 xaml 中绑定到源 (List) 的方式:
<DataGrid Name="dtgDecodedMsg"
ItemsSource="{Binding Path=MsgTypGridLineListVar, UpdateSourceTrigger=PropertyChanged}"
这是如何构建类的图片: Classes
简短说明: 我按下一个按钮,这个按钮用我想要显示的信息填充 MsgTypGridLineListVar。然后我调用 OnPropertyChanged 来更新 Datagrid。这行得通!
private void button_click(object parameter) {
this.MsgTypGridLineListVar = new List<CmsgTypGridLine>(); //Reset MsgTypGridLineListVar
... //Fill MsgTypGridLineListVar with information
OnPropertyChanged("MsgTypGridLineListVar");
}
现在我需要在 DataGrid 已填充时更改某些单元格的可见性/文本。所以它应该只更改某些字段/行,而不是创建新的。
所以我想的只是改变我想要的值然后打电话给OnPropertyChanged("MsgTypGridLineListVar");
再次。
但这不起作用..如果我向下滚动该行不再可见,然后再次向上滚动,它适用于单元格中的文本。但它不适用于可见性。
这是我创建的测试按钮:
private void testButton_click(object parameter)
{
this.MsgTypGridLineListVar[0].ByteCell.CellValue = "TEST";
this.MsgTypGridLineListVar[2].RowVisible = Visibility.Collapsed;
OnPropertyChanged("MsgTypGridLineListVar");
}
如前所述,它适用于文本(如果我向下滚动),但不适用于可见性。 我必须更改哪些更新会立即发生。
这是我的 Datagrid 的 xaml 代码,您可以在其中看到我是如何进行绑定的:
<DataGrid Name="dtgDecodedMsg"
CanUserSortColumns="False"
CanUserAddRows="False"
CanUserReorderColumns="False"
HeadersVisibility="Column"
IsTabStop="False"
ClipboardCopyMode="IncludeHeader"
SelectedIndex="{Binding DecodeSelectedGridIdx, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
ItemsSource="{Binding Path=MsgTypGridLineListVar, UpdateSourceTrigger=PropertyChanged}"
AutoGenerateColumns="False"
Margin="10,111,10,0">
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="..." Width="25">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="LightBlue"/>
<Setter Property="BorderThickness" Value="2"/>
</Trigger>
</Style.Triggers>
<Setter Property="Background" Value="{Binding HideCell.CellColor}"/>
<Setter Property="BorderBrush" Value="{Binding HideCell.CellColor}"/>
<Setter Property="Focusable" Value="{Binding HideCell.CheckBoxEnabled}"/>
</Style>
</DataGridCheckBoxColumn.CellStyle>
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox">
<Setter Property="Visibility" Value="{Binding HideCell.CheckBoxVisibility}"/>
<Setter Property="IsChecked" Value="{Binding CheckBoxChecked,UpdateSourceTrigger=PropertyChanged}" />
</Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
<DataGridTextColumn Header="Value" Binding="{Binding ValueTelegramCell.CellValue}" IsReadOnly="True" Width="*">
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="BorderBrush" Value="LightBlue"/>
<Setter Property="BorderThickness" Value="2"/>
</Trigger>
</Style.Triggers>
<Setter Property="Foreground" Value="{Binding ValueTelegramCell.TextColor}"/>
<Setter Property="Background" Value="{Binding ValueTelegramCell.CellColor}"/>
<Setter Property="BorderBrush" Value="{Binding ValueTelegramCell.CellColor}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="Visibility" Value="{Binding MsgTypGridLineListVar.RowVisible, UpdateSourceTrigger=PropertyChanged}"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
【问题讨论】: