gpt4 book ai didi

c# 和日期选择器,如何更改格式?

转载 作者:行者123 更新时间:2023-11-30 20:03:36 24 4
gpt4 key购买 nike

我的 C# 4.0 (WPF) 应用程序中有一个日期选择器,我想将文本框中可见的日期格式更改为 yyyy/MM/dd。现在我看到了格式 dd/MM/yyyy。

在我的 datePicker 的 axml 中,我有这段代码:

<DatePicker Height="25" HorizontalAlignment="Left" Margin="5,36,0,0" Name="dtpStartDate"
SelectedDate="{Binding StartDateSelectedDate}" 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>

这似乎是第一次一切正常,我可以看到我想要的格式的日期,我可以手动或使用日历更改日期,并且在这两种方式中到达 viewModel 的日期是正确的。

但是我有一个问题,因为我想检测如果日期为空,在我的 View 模型中控制这种情况。但是如果我清除日期选择器,在我的 View 模型中会到达最后一个正确的日期,所以我无法检查日期是否为空。

那么如何修改日期选择器中日期的格式并控制日期是否为空/null?

谢谢。戴姆洛克。

最佳答案

您可以尝试以下解决方案。

首先创建以下转换器:

public class StringToDateTimeConverter : IValueConverter
{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
{
return null;
}
return ((DateTime)value).ToString(parameter as string, CultureInfo.InvariantCulture);
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (string.IsNullOrEmpty(value as string))
{
return null;
}
try
{
DateTime dt = DateTime.ParseExact(value as string, parameter as string, CultureInfo.InvariantCulture);
return dt as DateTime?;
}
catch (Exception)
{
return null;
}
}
}

然后在 xaml 中,您必须创建转换器的实例并在 DatePicker 的文本框中使用它

<Window x:Class="TestDatePicker.MainWindow"
...
xmlns:converters="clr-namespace:TestDatePicker"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
...
<converters:StringToDateTimeConverter x:Key="StringToDateTimeConverter" />
</Window.Resources>
<Grid DataContext="{StaticResource MainWindowVM}">
...
<DatePicker Height="25" HorizontalAlignment="Left" Margin="5,36,0,0" Name="dtpStartDate"
SelectedDate="{Binding StartDateSelectedDate}" 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}}, Converter={StaticResource StringToDateTimeConverter}, ConverterParameter=yyyy/MM/dd}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
...
</Grid>

最后,在viewmodel中,属性必须是DateTime类型? (即可为空的 DateTime)。

    private DateTime? _startDateSelectedDate;
public DateTime? StartDateSelectedDate
{
get { return _startDateSelectedDate; }
set
{
if (_startDateSelectedDate != value)
{
_startDateSelectedDate = value;
RaisePropertyChanged(() => this.StartDateSelectedDate);
}
}
}

希望对你有帮助

问候

克劳德

关于c# 和日期选择器,如何更改格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14630318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com