在具有源绑定的 DataGridComboBoxColumn 期间忽略日期时间格式

问题描述

我有一个包含许多组合框的 Datagrid

我有两个 Datagrid。第一个显示 ListPerson List 的元素,列是 DataGridTextColum。第二个使用户可以使用 DataGridComboBoxColumn 更改 BirthDate 的值。除了 DateFormat,DataGridComboBoxColumn 工作正常。我无法按照我想要的方式对其进行格式化。

XAML

<Window.Resources>
   <CollectionViewSource x:Key="srcBirthDate" Source="{Binding CmbBirthDate}"/>
</Window.Resources>

<!-- First DataGrid -->
<DataGrid x:Name="dg_Person" AutoGenerateColumns="False"
    ItemsSource="{Binding ListPerson}" SelectedItem="{Binding PersonPicked}"
    CanUserSortColumns="True" CanUserResizeColumns="True" SelectionMode="Single"  
    CanUserDeleteRows="False" CanUserAddRows="False" IsReadOnly="True" >
    <DataGridTextColumn Header="BirthDate" Binding="{Binding BirthDate,StringFormat=\{0:yyyy.MM.dd\}}" />
</DataGrid>

<!-- Second DataGrid -->
<DataGrid x:Name="dg_PersonPick" AutoGenerateColumns="False"
    ItemsSource="{Binding ListPerson}" SelectedItem="{Binding PersonPicked}"
    CanUserSortColumns="True" CanUserResizeColumns="True" SelectionMode="Single"  
    CanUserDeleteRows="False" CanUserAddRows="False" IsReadOnly="True" >
    <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource srcBirthDate},StringFormat=\{0:yyyy.MM.dd\}}"  Header="BirthDate" displayMemberPath="RowValue" SelectedValuePath="RowValue" SelectedValueBinding="{Binding BirthDate,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
</DataGrid>

我的类型

public class DateTimeRow
{
    [Order]
    public DateTime? RowValue { get; set; }
}

在模型中:

private List<DateTimeRow> _cmbBirthDate;
public List<DateTimeRow> CmbBirthDate { get => _cmbBirthDate; set { _cmbBirthDate= value; OnPropertyChanged(); } }

我如何用 ListPerson 列表中的不同值填充列表:

_model.CmbBirthDate = _model.ListPerson.GroupBy(x => x.BirthDate).Select(x => x.FirstOrDefault()).Select(x => new DateTimeRow { RowValue = x.BirthDate}).ToList();

一个带有 DataGridTextColumn 的 Datagrid 按照我想要的方式显示日期:(yyyy.MM.dd) DataGrid DateTime Format

但看起来,带有 DataGridComboBoxColumn 的第二个 Datagrid 忽略了格式,但是给出了 StringFormat={0:yyyy.MM.dd}。 DataGrid with failed DateTime Format

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)