如何将绑定字符串格式设置为所有 DataGridTextColumn 控件? [英] how to set binding stringformat to all DataGridTextColumn controls?

查看:18
本文介绍了如何将绑定字符串格式设置为所有 DataGridTextColumn 控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 DataGridTextColumn 控件之一,如下所示:

This is one of my DataGridTextColumn controls looks like :

<DataGridTextColumn x:Name="contractStartDateColumn" Header="Start Date" Binding="{Binding Path=StartDate, StringFormat={}\{0:dd/MM/yyyy\}}" />

那我如何将 StringFormat={}{0:dd/MM/yyyy} 设置为所有 DataGridTextColumn 控件而不是设置每个控件?

Then how can I set StringFormat={}{0:dd/MM/yyyy} to all of DataGridTextColumn controls instead of setting every single one ?

推荐答案

您可以创建自定义绑定类来设置 StringFormat 并使用它来绑定值:

You can create custom binding class that sets StringFormat and use it to bind values:

public class CustomBinding : Binding
{
    public CustomBinding(string path) : base(path)
    {
        this.StringFormat = @"{0:dd/MM/yyyy}";
    }
}

在 XAML 中:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding TimeList}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{local:CustomBinding StartDate}" />
    </DataGrid.Columns>
</DataGrid>

这篇关于如何将绑定字符串格式设置为所有 DataGridTextColumn 控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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