![](/img/trans.png)
[英]Use IValueConverter within WPF DataGrid depending on several values of the same row
[英]IValueConverter, get other values of row in WPF DataGrid
我正在研究C#/ WPF項目。 我有一個綁定到模型的數據網格。
如何獲得轉換器中同一行的其他值?
轉換器示例:
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);
}
綁定完成后就可以在視圖(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>
如何獲得轉換器中當前行的其他值(例如“名稱”)?
在您的TextBoxColumn中,而不是綁定具有值屬性,而是綁定到該行的整個DataContext
<DataGridTextColumn Header="Value" Binding="{Binding ,NotifyOnTargetUpdated=True, Converter={StaticResource Converter}}">
然后在轉換器中,您將獲得整個對象支持您的DataGridRow。 假設模型是您的模型類
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.