【问题标题】:Send DataGrid ItemsSource as converterParameter将 DataGrid ItemsSource 作为转换器参数发送
【发布时间】:2017-07-11 06:29:20
【问题描述】:

在 WPF 中,我需要根据与此数据网格中其他行的比较为 dataGridRow 添加边框。

我无法更改源项目的属性,所以我想使用转换器来设置符合条件的行的样式。

如何将用作 DataGrid ItemsSource 的当前 ObservableCollection 作为转换器参数传递给转换器?

<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>

从上面的示例代码中,我想将 TableItems 作为 ConverterParameter 传递。

【问题讨论】:

标签: c# wpf datagrid


【解决方案1】:

您不能将任何内容绑定到 ConverterParameter 属性,因为它不是依赖属性。

您可以使用带有多个输入值的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>

或者您可以将依赖属性添加到转换器并将其绑定到您的 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>

【讨论】:

    【解决方案2】:

    由于您无法绑定 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 2021-12-17
      • 1970-01-01
      相关资源
      最近更新 更多