【问题标题】:Datagrid Propertychange of items in Itemssource does not update DatagridItemssource 中项目的 Datagrid Propertychange 不更新 Datagrid
【发布时间】: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>

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

    直接绑定到CmsgTypGridLineRowVisible属性:

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Visibility" Value="{Binding RowVisible}"/>
        </Style>
    </DataGrid.RowStyle>
    

    ...并确保CmsgTypGridLine 类实现INotifyPropertyChanged 接口并在RowVisible 属性的设置器中引发更改通知。

    【讨论】:

    • 谢谢!我忘了在我的子类中实现 INotifyPropertyChanged .. facepalm
    猜你喜欢
    • 2014-08-17
    • 2011-10-26
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2017-09-18
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多