我从这个论坛得到了这个示例代码。
<DatePicker SelectedDate="{Binding myVideModelProperty}"
Height="25" HorizontalAlignment="Left" Margin="81,-2,0,0" Name="myDatePicker" VerticalAlignment="Top" Width="115">
<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={}{0:yyyy/MM/dd}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
但是我希望yyyy/MM/dd的格式来自一个名为dateformat的应用程序属性。你能帮我把stringformat作为一个绑定到应用程序属性吗?
5条答案
按热度按时间esbemjvw1#
所有其他的答案看起来都有点复杂。它就像这样简单:
您需要做的就是调整字符串格式以适合自己。
h43kikqp2#
将此代码段用于DatePicker(您必须设置其样式):
将此代码段用于App.xaml.cs(将命名空间更改为有用的名称):
在包含DatePicker的xaml控件中,定义XAML命名空间,如下所示:
xmlns:local="clr-namespace:_24417730"
请注意,您必须将命名空间变更为用来定义应用程序类别的命名空间。
下面是我如何在MainWindow.xaml控件上定义xaml命名空间的:
此外,阅读Static Markup Extension和XAML Namespaces可能有助于将来的参考。
如果您需要进一步的说明,我将很乐意为您提供帮助。
wztqucjr3#
您可以使用转换器来实现这一点。或者您可以向类中添加另一个属性,它将返回带格式的日期。
带变频器
llmtgqce4#
yquaqz185#
为
DatePickerTextBox
添加一个Style
,如下所示,不需要CultureInfo
,使用StringFormat
生成所需的格式: