user2799180
user2799180

Reputation: 749

IValueConverter, get other values of row in WPF DataGrid

I'm working on an C#/WPF project. I have a datagrid binded to a model.

How can I get the other values of the same row available within the converter?

Converter example:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    int val = (int)value;

    return (int)(val / 0.41);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    int val = (int)value;
    return (int)(val * 0.41);
}

Binding is done like can be seen in the View (XAML):

<DataGrid ItemsSource="{Binding FixParaCollectionView}"  AutoGenerateColumns="False">
<DataGrid.Columns>
      <DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Name}"/>
       <DataGridTextColumn Header="Value" Binding="{Binding Value,NotifyOnTargetUpdated=True, Converter={StaticResource Converter}}">
       </DataGridTextColumn>
</DataGrid.Columns>

How could I get other values (e.g. "Name") of the current row within the converter?

Upvotes: 0

Views: 4432

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18580

In your TextBoxColumn, instead of Binding with Value proeperty bind to the whole DataContext of the row

<DataGridTextColumn Header="Value" Binding="{Binding ,NotifyOnTargetUpdated=True, Converter={StaticResource Converter}}">

Then in converter you will get the whole object backing your DataGridRow. Assuming Model is your model class

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    Model model= value as Model;

    return (int)(model.Value/ 0.41);
}

Upvotes: 2

Related Questions