简体   繁体   中英

WPF, Datagrid cell is changing datepicker dateformat

The problem which i struggle with is the following: I have a datepicker inside a datagrid template column, after i select the date in the datepicker which has the format requested by me and that is yyyy-MM-dd, then immediatly in the datagrid cell the format changes, to dd/MM/yyyy hh:mm:ss tt. (this looks like the local windows datetime format which i am running now) What am i doing wrong and why the grid cell changes the format. This is the XAML:

<DataGridTemplateColumn Header="DueDate">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding XPath=//cbc:DueDate | //cac:PaymentMeans/cbc:PaymentDueDate}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding XPath=//cbc:DueDate | //cac:PaymentMeans/cbc:PaymentDueDate}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>

This is a screen shot with the datePicker: enter image description here

This is the changed result in the datagrid cell. enter image description here

xaml headers:

   <Window.Resources>
    <local:StringToDateConverter x:Key="StringToDateConverter" />
    <XmlNamespaceMappingCollection x:Key="mapping">
        <XmlNamespaceMapping Uri="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" Prefix="cac" />
        <XmlNamespaceMapping Uri="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" Prefix="cbc" />
        <XmlNamespaceMapping Uri="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" Prefix="i" />
        <XmlNamespaceMapping Uri="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2" Prefix="c" />
    </XmlNamespaceMappingCollection>
    <XmlDataProvider x:Name="dataprovider" XmlNamespaceManager="{StaticResource mapping}" x:Key="xmlProvider" />
    <Style x:Key="DataGridCellStyle"  TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=Content.Text, RelativeSource={RelativeSource Self}}"  Value="" >
                <Setter Property="Background" Value="Red"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>

DatePicker formats its dates a certain way, but after you finish editing the cell, the DatePicker goes away and is replaced by a TextBlock (it switches from CellEditingTemplate to CellTemplate ). TextBlock will just use the default formatting when converting the date unless you tell it otherwise.

Try this:

<TextBlock Text="{Binding XPath=//cbc:DueDate | //cac:PaymentMeans/cbc:PaymentDueDate, StringFormat={}yyyy-MM-dd}}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM