简体   繁体   English

IValueConverter,获取WPF DataGrid中行的其他值

[英]IValueConverter, get other values of row in WPF DataGrid

I'm working on an C#/WPF project. 我正在研究C#/ WPF项目。 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): 绑定完成后就可以在视图(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 在您的TextBoxColumn中,而不是绑定具有值属性,而是绑定到该行的整个DataContext

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

Then in converter you will get the whole object backing your DataGridRow. 然后在转换器中,您将获得整个对象支持您的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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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