When I assign DataGridDateTimeColumn.Format to a Binding, it gets its initial value from the binding. However, if the binding source changes, the format does not change.
For a test project, I created a new WPF project in Visual Studio, and set the code as shown below. If you change the format string in the TextBox, then the format in the standalone DateTimeEditBox updates as expected. However, the format in the DataGridDateTimeColumn of the first DataGrid remains stuck on the initial value of the format. In the second DataGrid, I assigned a binding through the ElementStyle. In the second DataGrid, the format does update as desired.
Although I can use the ElementStyle as shown in the second DataGrid, I expected that the DataGridDateTimeColumn.Format property should have taken care of this. Am I missing something?
<Window
x:Class="DataGridColumn.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:editors="http://schemas.actiprosoftware.com/winfx/xaml/editors"
xmlns:datagrideditors="http://schemas.actiprosoftware.com/winfx/xaml/datagrideditors"
Title="DataGridColumnExperiment" Height="200" Width="600">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBox Width="150" x:Name="FormatStringTextBox" Margin="12"
Text="{Binding DateFormat, UpdateSourceTrigger=PropertyChanged}"/>
<editors:DateTimeEditBox
Grid.Row="1"
Format="{Binding DateFormat}" />
<DataGrid
x:Name="DataGrid1"
Grid.Column="1" Grid.RowSpan="2" Margin="2"
AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="False">
<DataGrid.Columns>
<datagrideditors:DataGridDateTimeColumn
Binding="{Binding Mode=OneWay}"
Format="{Binding DataContext.DateFormat, Source={x:Reference FormatStringTextBox}}" />
</DataGrid.Columns>
</DataGrid>
<DataGrid
x:Name="DataGrid2"
Grid.Column="2" Grid.RowSpan="2" Margin="2"
AutoGenerateColumns="False"
CanUserAddRows="False" CanUserDeleteRows="False" CanUserSortColumns="False">
<DataGrid.Columns>
<datagrideditors:DataGridDateTimeColumn
Binding="{Binding Mode=OneWay}" >
<datagrideditors:DataGridDateTimeColumn.ElementStyle>
<Style TargetType="editors:DateTimeEditBox">
<Setter Property="Format"
Value="{Binding DataContext.DateFormat,
Source={x:Reference FormatStringTextBox}}" />
</Style>
</datagrideditors:DataGridDateTimeColumn.ElementStyle>
</datagrideditors:DataGridDateTimeColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataGrid1.ItemsSource = new List<DateTime>
{
new DateTime(1818, 10,3,5,8,34),
new DateTime(1973, 4,8,15,45,12),
new DateTime(2000, 10,13,13,13,55),
new DateTime(2028, 1,31,22,8,4)
};
DataGrid2.ItemsSource = DataGrid1.ItemsSource;
DataContext = this;
}
public static readonly DependencyProperty DateFormatProperty =
DependencyProperty.RegisterAttached("DateFormat",
typeof(String),
typeof(MainWindow),
new UIPropertyMetadata("d"));
public String DateFormat
{
get { return (String)GetValue(DateFormatProperty); }
set { SetValue(DateFormatProperty, value); }
}
}