【问题标题】:WPF - How to apply converter to all DataGridTextColumn?WPF - 如何将转换器应用于所有 DataGridTextColumn?
【发布时间】:2018-03-30 19:24:46
【问题描述】:

我想使用 WPF Apply Converter to Binding Value 到应用程序中的所有 DataGridTextColumn。

对于单个 DataGridTextColumn 转换器工作正常:

<DataGridTextColumn 
    Header ="Value" 
    Binding="{Binding Value, Converter={StaticResource decimalConverter}}" 
    />

但在应用程序中,我在不同的 DataGrid 中获得了许多(超过 100 个)DataGridTextColumn,我会知道最好的解决方案,而不是分别应用每个列转换器。

我知道使用 Style 可以修改所有控件类型(例如前景)的某些属性,但不确定如何将这些用于绑定值和转换器?

【问题讨论】:

  • 在我的情况下,当出现类似问题时,我最终修改了您的示例中的每一个,然后追查任何剩余的。无聊且容易出错,但 WPF 想不出比这更好的了。
  • 所以你想在所有DataGridTextColumn.Bindings中添加一个转换器,甚至是非十进制的?
  • 为 DataGridTextColumn 设置样式并不容易。 DataGridTextColumn 不继承自 FrameworkElement。
  • 是的,为了澄清在这些应用程序中我只有十进制 DataGridTextColumns

标签: c# wpf converter


【解决方案1】:

您可以借助全局样式和附加属性来实现。您不能为DataGridTextColumn 创建全局样式(或任何样式),因为它不是从FrameworkElement 继承的。但是您可以为DataGrid 本身创建样式,以该样式为网格设置附加属性,并在添加所有列绑定时在该附加属性集转换器的属性更改处理程序中。示例代码:

public class DataGridHelper : DependencyObject {
    public static IValueConverter GetConverter(DependencyObject obj) {
        return (IValueConverter) obj.GetValue(ConverterProperty);
    }

    public static void SetConverter(DependencyObject obj, IValueConverter value) {
        obj.SetValue(ConverterProperty, value);
    }

    public static readonly DependencyProperty ConverterProperty =
        DependencyProperty.RegisterAttached("Converter", typeof(IValueConverter), typeof(DataGridHelper), new PropertyMetadata(null, OnConverterChanged));

    private static void OnConverterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        // here we have our converter
        var converter = (IValueConverter) e.NewValue;
        // first modify binding of all existing columns if any
        foreach (var column in ((DataGrid) d).Columns.OfType<DataGridTextColumn>()) {
            if (column.Binding != null && column.Binding is Binding)
            {
                ((Binding)column.Binding).Converter = converter;
            }
        }
        // then subscribe to columns changed event and modify binding of all added columns
        ((DataGrid) d).Columns.CollectionChanged += (sender, args) => {
            if (args.NewItems != null) {
                foreach (var column in args.NewItems.OfType<DataGridTextColumn>()) {
                    if (column.Binding != null && column.Binding is Binding) {
                        ((Binding) column.Binding).Converter = converter;
                    }
                }
            }
        };
    }
}

然后在某处创建全局样式(如 App.xaml):

<Application.Resources>
    <local:TestConverter x:Key="decimalConverter" />
    <Style TargetType="DataGrid">
        <Setter Property="local:DataGridHelper.Converter"
                Value="{StaticResource decimalConverter}" />
    </Style>
</Application.Resources>

【讨论】:

  • 它对我来说非常好用!感谢您的快速解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-16
  • 2016-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多