简体   繁体   中英

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 (eg "Name") of the current row within the converter?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM