WPF DatePicker 自定义格式
WPF DatePicker Custom Format
如前所述,我希望将日期选择器日期自定义为 YYYY-MM-DD
我的 XAML 文件中有:
<DatePicker x:Name="DatePicker_Date" Width="300px" VerticalAlignment="Center" HorizontalAlignment="Center" >
<DatePicker.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate,
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}},
StringFormat={x:Static local:App.DateFormat}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
然后在我的 App.xaml.cs 文件中我有:
public partial class App : Application
{
public static string DateFormat = "yyyy-MM-dd";
}
所以这可以在选择器本身内以正确的方式显示日期,但是当我去引用那个日期时:
string dateTemp = DatePicker_Date.Text;
只是打印出原来的日期格式MM-DD-YYYY
我在这里遗漏了什么 -- 我不能使用不起作用的 PART_Textbox.text。如何将新格式化的文本转换为字符串?
我已经遵循了其中一些关于堆栈溢出的解决方案,但它们似乎有相同的结果,我最喜欢的是在 app.xaml 资源中设置的,所以我不必每次都这样做我的应用程序中的日期选择器,但我有同样的问题:
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<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>
对 WPF 比较陌生,请原谅我的无知。
您应该使用 SelectedDate
属性 将当前选择的日期作为 DateTime
获取,然后根据您的特定格式对其进行格式化:
string dateTemp = DatePicker_Date.SelectedDate?.ToString(App.DateFormat);
如前所述,我希望将日期选择器日期自定义为 YYYY-MM-DD 我的 XAML 文件中有:
<DatePicker x:Name="DatePicker_Date" Width="300px" VerticalAlignment="Center" HorizontalAlignment="Center" >
<DatePicker.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox"
Text="{Binding Path=SelectedDate,
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}},
StringFormat={x:Static local:App.DateFormat}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
然后在我的 App.xaml.cs 文件中我有:
public partial class App : Application
{
public static string DateFormat = "yyyy-MM-dd";
}
所以这可以在选择器本身内以正确的方式显示日期,但是当我去引用那个日期时:
string dateTemp = DatePicker_Date.Text;
只是打印出原来的日期格式MM-DD-YYYY
我在这里遗漏了什么 -- 我不能使用不起作用的 PART_Textbox.text。如何将新格式化的文本转换为字符串?
我已经遵循了其中一些关于堆栈溢出的解决方案,但它们似乎有相同的结果,我最喜欢的是在 app.xaml 资源中设置的,所以我不必每次都这样做我的应用程序中的日期选择器,但我有同样的问题:
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="VerticalContentAlignment" Value="Center"/>
<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>
对 WPF 比较陌生,请原谅我的无知。
您应该使用 SelectedDate
属性 将当前选择的日期作为 DateTime
获取,然后根据您的特定格式对其进行格式化:
string dateTemp = DatePicker_Date.SelectedDate?.ToString(App.DateFormat);