Willy
Willy

Reputation: 10660

WPF binding selected item in datagrid to a second datagrid and show value depending of it

I have two WPF datagrids. When a row is selected from first datagrid, dg1, a column called 'Notes' in second datagrid, dg2, should be shown as empty strings only and only if the content of the column 'Note Type' for the selected row in dg1 is equal to "I". Otherwise, content of 'Notes' column in dg2 should be the one from collection ItemsDg2 that comes from Notes.

My problem is that when value is "I", the content of 'Note Type' column in dg1 is shown in column 'Notes' of dg2 instead of showing an empty string.

<Window x:Name="MainWindow"
        xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib">


   <my:DataGrid Name="dg1" ItemsSource="{Binding Path=ItemsDg1}">
       <my:DataGrid.Columns>
          <my:DataGridTextColumn x:Name="iType" Binding="{Binding Path=Type}" Header="Note Type"/>
       </my:DataGrid.Columns>   
   </my:DataGrid>

   <my:DataGrid Name="dg2" ItemsSource="{Binding Path=ItemsDg2}">      
       <my:DataGrid.Columns>
            <my:DataGridTextColumn Binding="{Binding Path=Notes}" Header="Notes">
                <my:DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">                                       
                        <Setter Property="Text" Value="{Binding Path=Notes}"></Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem.Type, ElementName=dg1}" Value="I">
                                <Setter Property="Text" Value="{x:Static sys:String.Empty}"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </my:DataGridTextColumn.ElementStyle>
            </my:DataGridTextColumn>
        </my:DataGrid.Columns>          
   </my:DataGrid>

</Window>

ItemsDg1 is a List of It1

ItemsDg2 is a List of It2

public Class It1
{

    private string _type;
    public string Type
    {
        get { return _type; }
        set
        {
            if (!_type.Equals(value))
            {
                _type = value;
            }
        }
    }

}

public Class It2
{

    private string _notes;
    public string Notes
    {
        get { return _notes; }
        set
        {
            if (!_notes.Equals(value))
            {
                _notes = value;
            }
        }
    }

}

Classes It1 and It2 have more properties but I only show here the minimum to understand the scenario. The same applies to dg1 and dg2, they have more datagrid columns.

Upvotes: 0

Views: 386

Answers (1)

Mike Strobel
Mike Strobel

Reputation: 25623

If I understand you correctly, you should get the results you want by replacing the Notes column definition in dg2 as follows:

<DataGridTemplateColumn Header="Notes">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <TextBlock x:Name="CellText" Text="{Binding Notes}" />
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding SelectedItem.Type, ElementName=dg1}" Value="I">
          <Setter TargetName="CellText" Property="Text" Value="" />
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

Upvotes: 0

Related Questions