DataGridTextColum绑定StringFormat [英] DataGridTextColum binding StringFormat

查看:51
本文介绍了DataGridTextColum绑定StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将视图模型中的字符串绑定到DataGridTextColumn的StringFormat属性.但是,我下面的内容不起作用.有没有办法在不使用转换器或资源的情况下做到这一点?如果没有,最简单的方法是什么?

I am trying to bind a string in my view model to the StringFormat property of the DataGridTextColumn. But, what I have below is not working. Is there a way to do this without using converters or resources? If not, what's the easiest way to do it?

<DataGridTextColumn Binding="{Binding Date, StringFormat={Binding DateFormat}}" />

推荐答案

您不能将任何内容绑定到 Binding 的属性,因为 Binding 类不继承自 DependencyObject .

You can't bind anything to properties of Binding because the Binding class doesn't inherit from DependencyObject.

控件具有您可以绑定的 ContentStringFormat 属性.如果 DataGridTextColumn 是从 ContentControl 派生的,那将解决您的问题,但事实并非如此.

Controls derived from ContentControl (e.g. Label) have a ContentStringFormat property that you can bind. That would solve your problem in this case if DataGridTextColumn were derived from ContentControl but it isn't.

您可以将其设置为带有包含 Label DataTemplate DataGridTemplateColumn ,然后将 Label.ContentStringFormat 绑定到您的 DateFormat 属性:

You could make it a DataGridTemplateColumn with a DataTemplate containing a Label, and bind Label.ContentStringFormat to your DateFormat property:

<DataGridTemplateColumn Header="Date Template">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Label
                Content="{Binding Date}"
                ContentStringFormat="{Binding DataContext.DateFormat, 
                    RelativeSource={RelativeSource AncestorType=DataGrid}}"
                />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

但是当我更改viewmodel的 DateFormat 属性时,这不会更新.我不知道为什么不这样做,可能是我做错了.

But that doesn't update when I change the viewmodel's DateFormat property. I don't know why not, possibly I did something wrong.

这给我们留下了一个多值转换器.当我更改viewmodel的 DateFormat 属性(两个相邻的列,一个更新而另一列没有更新)时,该更新确实会更新-所以没有人告诉我我没有提出 PropertyChanged ).

That leaves us with a multi-value converter. That does update when I change the viewmodel's DateFormat property (two adjacent columns, one updates and the other doesn't -- so don't anybody tell me I didn't raise PropertyChanged).

<Window.Resources>
    <local:StringFormatConverter x:Key="StringFormatConverter" />
</Window.Resources>

...等等等等...

...blah blah blah...

<DataGridTextColumn
    Header="Date Text" 
    >
    <DataGridTextColumn.Binding>
        <MultiBinding Converter="{StaticResource StringFormatConverter}">
            <Binding Path="Date" />
            <Binding 
                Path="DataContext.DateFormat" 
                RelativeSource="{RelativeSource AncestorType=DataGrid}" />
        </MultiBinding>
    </DataGridTextColumn.Binding>
</DataGridTextColumn>

C#:

这会将任何字符串格式应用于任何值,而不仅仅是 DateTime .我的 DateFormat 属性返回的是"{0:yyyy-MM-dd}" .

This will apply any string format to any value, not just a DateTime. My DateFormat property is returning "{0:yyyy-MM-dd}".

此转换器上的第一个绑定是您要格式化的值.

The first binding on this converter is the value you want to format.

第二个绑定是 {0} -并且,如果该值为 DateTime ,则将其格式化为四个位数年份,破折号,两位数的月份,破折号和两位数的日期. DateTime 格式字符串本身是一个主题;这是该主题上的MSDN页面.

The second binding is a format string parameter for String.Format(). The above format string takes the "zeroth" parameter after the format string -- that's {0} -- and, if that value is a DateTime, formats it with a four digit year, a dash, a two digit month, a dash, and a two digit day. DateTime format strings are a subject unto themselves; here's the MSDN page on the subject.

我给你的东西是最简单的写方法,而最强大的就是它.您可以传入一个双精度型,并给它一个格式字符串,例如我的演员表有{0:c}脚趾" ,如果双精度型是 3.00999 ,它会告诉您它的猫的脚趾 $ 3.01 .

What I've given you is the simplest way to write this, and by a vast margin the most powerful. You can pass in a double and give it a format string like "My cast has {0:c} toes" and if the double is 3.00999, it'll tell you its cat has $3.01 toes.

但是,在为您提供 String.Format()的全部功能时,我使编写格式字符串的业务变得有些复杂.这是一个权衡.

However, in giving you all the power of String.Format(), I've somewhat complicated the business of writing your format strings. It's a tradeoff.

public class StringFormatConverter : IMultiValueConverter
{
    public object Convert(object[] values, 
        Type targetType, object parameter, CultureInfo culture)
    {
        return String.Format((String)values[1], values[0]);
    }

    public object[] ConvertBack(object value, 
        Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这篇关于DataGridTextColum绑定StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆