kpischi
kpischi

Reputation: 35

Datagrid Propertychange of items in Itemssource does not update Datagrid

I have a problem with my Datagrid. If I hange a property in my source-class it does not update.

This is how the Binding to the Source ( List ) is done in xaml :

<DataGrid Name="dtgDecodedMsg"
                ItemsSource="{Binding Path=MsgTypGridLineListVar, UpdateSourceTrigger=PropertyChanged}" 

Here is a picture how the classes are built : Classes

Short description : I press a Button and this Button fills the MsgTypGridLineListVar with the information I want to show. Then I call the OnPropertyChanged to update the Datagrid. This is working !

private void button_click(object parameter) {
    this.MsgTypGridLineListVar = new List<CmsgTypGridLine>(); //Reset MsgTypGridLineListVar
    ... //Fill MsgTypGridLineListVar with information
    OnPropertyChanged("MsgTypGridLineListVar"); 
}

Now I need to change the Visibility / text of some cells when the DataGrid is already filled. So it should change just certain fields/rows and not be created new. So what i thought was just changing the values i want and then call OnPropertyChanged("MsgTypGridLineListVar"); again.

But this does not work.. It works for text in a Cell if I scroll down that this row is not visible anymore and then scroll up again. But it does not work for the Visibility.

Here is a test-button I created:

private void testButton_click(object parameter)
{

  this.MsgTypGridLineListVar[0].ByteCell.CellValue = "TEST";
  this.MsgTypGridLineListVar[2].RowVisible = Visibility.Collapsed;
  OnPropertyChanged("MsgTypGridLineListVar");
}

As mentioned it works for the Text (if I scroll down) but not for the Visibility. What do I have to change that the Update happens immediately.

Here is my xaml-Code for the Datagrid where you can see how I do the Binding:

<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>

Upvotes: 0

Views: 304

Answers (1)

mm8
mm8

Reputation: 169400

Bind directly to the RowVisible property of the CmsgTypGridLine:

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

...and make sure that the CmsgTypGridLine class implements the INotifyPropertyChanged interface and raise change notifications in the setter of the RowVisible property.

Upvotes: 1

Related Questions