以编程方式将转换器绑定到DataGrid中的列

时间:2016-01-26 20:05:07

标签: wpf data-binding wpfdatagrid

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

新要求是“必需”属性。对于所需的列,我想绑定一个转换器,根据值设置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>

(仍然需要在某处添加转换器....)
或者我想要做什么必须在代码隐藏中完成?任何指针都会非常感激......

1 个答案:

答案 0 :(得分:0)

这是一种做法。 IDK,如果它是最好的方式,但是它有效并且自从你提出要求以来已经过了几个小时....

您的DataGridCell充满了边框/文本框,所以我假设您想要更改文本框的背景颜色,因为您无法看到DataGridCell的背景。

由于您提到将来会出现更复杂的情况,我使用带转换器的多重绑定并传入文本框datacontext(使用&lt; Binding /&gt;)及其文本值。

<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();
    }
}

并且,截图:

enter image description here