4

これは私のDataGridTextColumnコントロールの1つです:

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

次に、すべてのコントロールを設定するのではなく、 StringFormat = {} {0:dd / MM / yyyy}をすべてのDataGridTextColumnコントロールに設定するにはどうすればよいですか?

4

2 に答える 2

6

StringFormat次の値を設定および使用して値をバインドするカスタムバインディングクラスを作成できます。

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>
于 2011-11-28T07:13:54.437 に答える
3

StringFormatは.csファイルの定数に保存できます。xamlでは次を使用します

<DataGridTextColumn x:Name="contractStartDateColumn" Header="Start Date" Binding="{Binding Path=StartDate, StringFormat={x:static MyNamespace:MyClass.MyDateFormat}}" />
于 2011-11-28T12:36:27.610 に答える