Datepicker输出格式Datagrid wpf

时间:2017-01-23 16:42:56

标签: wpf xaml datagrid

我尝试得到一个像这样的“01.01.1999”的简单格式 - 但总之,结束后编辑结果(视觉和真实)值是“01/01/1999 12:00:00 AM”)它看起来像,忽略“StringFormat ='dd MM yyyy'”。我该如何解决这个问题?

 <DataGridTemplateColumn Header="von">
<DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock  Text="{Binding=von, StringFormat='dd MM yyyy'}"/>
                            </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
                                <DatePicker  SelectedDate="{Binding von, StringFormat='dd MM yyyy'}"/>
    </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

2 个答案:

答案 0 :(得分:1)

你试过这个吗?:

<DatePicker  SelectedDate="{Binding von}">
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd MM yyyy', 
                                    RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>

...或者此处的任何其他建议?:

Changing the string format of the WPF DatePicker

答案 1 :(得分:-1)

解决方案非常简单:

我将“von”从“String”改为“DateTime”

public DateTime von { get; set; } = DateTime.Now;

最后这对我有用:

<DataGridTemplateColumn Header="von">
<DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock  Text="{Binding von, StringFormat='dd MM yyyy'}"/>
                            </DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
                                <DatePicker SelectedDate="{Binding von}"/>

                            </DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>