[英]Send DataGrid ItemsSource as converterParameter
In WPF, I need to add border to a dataGridRow based on comparison with other rows in this data grid. 在WPF中,我需要根据与该数据网格中其他行的比较向dataGridRow添加边框。
I can't alter the source items properties, so I would like to use a converter to set the style of rows that match a condition. 我无法更改源项目的属性,因此我想使用转换器来设置符合条件的行的样式。
How can I pass current ObservableCollection that is used as DataGrid ItemsSource to a converter as a converterParameter? 如何将用作DataGrid ItemsSource的当前ObservableCollection作为converterParameter传递给转换器?
<DataGrid ItemsSource="{Binding TableItems}" Name="TableDataGrid" AutoGenerateColumns="False" BorderThickness="0">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Setter Property="BorderBrush" Value="#FF9E9E9E"/>
<Setter Property="BorderThickness" Value="{Binding Path=., Converter={StaticResource converter}, ConverterParameter= ??? }"/>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>
...
</DataGrid.Columns>
</DataGrid>
From sample code above, I would like to pass TableItems as ConverterParameter. 从上面的示例代码中,我想将TableItems作为ConverterParameter传递。
Since you can't bind the ConverterParameter
property of a Binding, you should use a MultiBinding
and make your converter implement IMultiValueConverter
. 由于无法绑定Binding的
ConverterParameter
属性,因此应使用MultiBinding
并使转换器实现IMultiValueConverter
。
<Setter Property="BorderThickness">
<Setter.Value>
<MultiBinding Converter="{StaticResource converter}">
<Binding Path="."/>
<Binding Path="DataContext.TableItems"
RelativeSource="{RelativeSource AncestorType=DataGrid}"/>
</MultiBinding>
</Setter.Value>
</Setter>
You can't bind anything to the ConverterParameter
property since it is not a dependency property. 您不能将任何内容绑定到
ConverterParameter
属性,因为它不是依赖项属性。
You could either use an IMultiValueConverter that takes several input values: 您可以使用带有多个输入值的IMultiValueConverter :
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Setter Property="BorderBrush" Value="#FF9E9E9E"/>
<Setter Property="BorderThickness">
<Setter.Value>
<MultiBinding Converter="{StaticResource multiConverter}">
<Binding Path="." />
<Binding Path="DataContext.TableItems" RelativeSource="{RelativeSource AncestorType=DataGrid}" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</DataGrid.ItemContainerStyle>
Or you could add a depenendency property to your converter and bind this one to the ItemsSource collection of your DataGrid
: 或者,您可以将依赖关系属性添加到转换器中,然后将此属性绑定到
DataGrid
的ItemsSource集合中:
public class DataGridConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty SourceCollectionProperty =
DependencyProperty.Register("SourceCollection", typeof(IEnumerable),
typeof(DataGridConverter), new FrameworkPropertyMetadata(null));
public IEnumerable SourceCollection
{
get { return (IEnumerable)GetValue(SourceCollectionProperty); }
set { SetValue(SourceCollectionProperty, value); }
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<DataGrid ItemsSource="{Binding TableItems}" Name="TableDataGrid" AutoGenerateColumns="False" BorderThickness="0">
<DataGrid.Resources>
<local:DataGridConverter x:Key="converter" SourceCollection="{Binding Path=DataContext.TableItems, Source={x:Reference TableDataGrid}}" />
</DataGrid.Resources>
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<Setter Property="BorderBrush" Value="#FF9E9E9E"/>
<Setter Property="BorderThickness" Value="{Binding Path=., Converter={StaticResource converter}}"/>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.