【问题标题】:Programmatically bind converter to column in DataGrid以编程方式将转换器绑定到 DataGrid 中的列
【发布时间】:2016-01-26 20:05:06
【问题描述】:

我希望我能解释清楚....
我们将DataGrid 绑定到来自某个数据源的集合。 每列的属性在不同的集合中描述,因此我们在运行时创建列并根据属性集合中的值设置列的属性(例如只读)。

新要求是“必需”属性。对于所需的列,我想绑定一个转换器,该转换器根据该值设置 DataGridCell 的背景颜色。 (如果单元格为空,转换器最简单的情况是某种颜色,如果用户输入一个值,则为白色。我相信将来会出现更复杂的验证。)

我认为它可以用我现在正在修补的东西来完成:

<Style TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                    <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}">
                    </TextBox>
                 </Border>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>

(仍然需要在某处添加转换器....)
还是我想做的事情必须在代码隐藏中完成?任何指针将不胜感激......

【问题讨论】:

    标签: wpf data-binding wpfdatagrid


    【解决方案1】:

    这是一种方法。 IDK 如果这是最好的方法,但它确实有效,而且距离你这么问已经过去了几个小时......

    您的 DataGridCell 填充了边框/文本框,因此我假设您想更改文本框的背景颜色,因为您看不到 DataGridCell 的背景。

    由于您提到将来可能会有更复杂的场景,因此我使用了带有转换器的多重绑定,并传入了文本框数据上下文(通过使用 )和它的文本值。

    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}">
                            <TextBox.Resources>
                                <local:ValidationBGConverter x:Key="ValidationBGConverter" />
                            </TextBox.Resources>
                            <TextBox.Style>
                                <Style TargetType="TextBox">
                                    <Setter Property="Background">
                                        <Setter.Value>
                                            <MultiBinding Converter="{StaticResource ValidationBGConverter}">
                                                <Binding />
                                                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content.Text" />
                                            </MultiBinding>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </TextBox.Style>
                        </TextBox>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    这是转换器:

    public class ValidationBGConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.Length != 2)
                return Brushes.Black;
            var datacontext = values[0] as ViewData; // Or whatever the textbox's datacontext object is
            if (datacontext != null) // If null, probably the new item row
            {
                var txt = values[1] as string; // Textbox text
                if (string.IsNullOrWhiteSpace(txt))
                    return Brushes.Red;
                if (txt.Length < 3)
                    return Brushes.Pink;
                if (txt.Length > 5)
                    return new LinearGradientBrush(Colors.White, Colors.Blue, 90.0);
            }
            return Brushes.White;
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    还有,截图:

    【讨论】:

    • 非常有帮助,谢谢。特别是关于将 DataContext 作为参数传递的部分:.
    猜你喜欢
    • 2014-04-18
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 2016-06-17
    • 1970-01-01
    • 2013-06-29
    • 2014-09-05
    • 2012-02-23
    相关资源
    最近更新 更多