的DateTimePicker WPF + + +绑定接受通过userinput DD / MM / YYYY [英] DateTimePicker + WPF + binding + accept dd/MM/yyyy via userinput
问题描述
简体问:
我在WPF一个datepicker。我在日期选择器的文本框中输入一个值30/10/1983。我需要被调回我的视图模型的价值,在那里我有它绑定到一个日期时间?属性。
I have a datepicker in WPF. I enter a value 30/10/1983 in the textbox of the datepicker. I need the value to be posted back to my viewmodel, where in I have bound it to a DateTime? property.
有什么办法,我可以做到这一点。毫米/ dd / yyyy格式触发回发,但不是DD / MM / YYYY。
Is there any way I can achieve this. mm/dd/yyyy format triggers a postback but not dd/mm/yyyy.
DatePicker的代码如下所示。
The DatePicker code is as below.
<DatePicker Grid.Row="2" Name="inputDate"
Text="{Binding BirthDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
>
</DatePicker>
视图模型属性如下图所示。
The view model property is as below.
private DateTime? birthDate;
public DateTime? BirthDate
{
get
{
return this.birthDate;
}
set
{
this.birthDate = value;
OnPropertyChanged("BirthDate");
}
}
我输入一个值10/10到日期选择器的文本框,二传手被称为具有价值,但我输入整个日期30/10/1983的那一刻,我还是有它等于NULL视图模型属性。
I enter a value 10/10 into the datepicker textbox, the setter gets called with a value, but the moment I enter an entire date 30/10/1983, I still have the view model property which is equal to NULL.
我已经改变了格式,以英文UnitedKingdom的买家,在日历设置,我的CurrentCulture恰如其分地反映了EN-GB。
I have changed the Format to English-UnitedKingdom, in the calendar settings, and my CurrentCulture reflects en-GB appropriately.
原题:
我在WPF DateTimePicker控件,这是我已绑定视图模型中的文本属性设置为可空日期时间。
I have a datetimepicker control in WPF, which I have bound the text property to a Nullable DateTime in the view model.
我现在面临的问题是,当我的格式输入一个值MM / DD / YYYY,我做得到的视图模型值回传,表示新的价值。
The problem I am facing is when I enter a value in the format MM/dd/yyyy, I do get a value postback in the view model, indicating the new value.
但是,当我在DD输入值/ MM / YYYY格式,我没有得到通知在视图模型和值被丢失。中束缚propertyin视图模型为空
But when I enter the value in dd/MM/yyyy format, I do not get a notification in the view model and the value is lost. The bound propertyin the view model is null.
短日期格式是我在日历设置和放大器已指定的;日期格式,其内的短日期格式我公司提供的条目DD / MM / YYYY。
The short date format is the one which I have specified in the calendar settings & dateFormat, within which for a short date format I provide the entry as "dd/MM/yyyy".
可能有人请帮我这个场景,在我接受日期在DD / MM / YYYY格式,我不想硬编码,我想从日历设置拿起短日期格式,另外,我希望收到的视图模型更新的值了。
Could someone please help me with this scenario where in I accept date in dd/MM/yyyy format, and I do not want to hardcode, I wish to pick up the short date format from the calendar settings and also, I wish to recieve the updated value in the view model too.
推荐答案
的问题与一个无效的日期将不会设定一个正确的值您的视图模型的DateTime财产。所以,你可以拦截它,并正确地转换器转换
The problem is related that a not valid date will not set with a correct value your ViewModel DateTime property. So, you can intercept it and convert correctly with a CONVERTER.
一个例子:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = System.Convert.ToString(value);
DateTime resultDateTime;
if (DateTime.TryParse(strValue, out resultDateTime))
{
return resultDateTime;
}
return value;
}
和您的XAML代码将是这样的:
And your XAML code will be like:
<DatePicker
Text="{Binding BirthDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text,
Converter={StaticResource DateConverter}}"
/>
这篇关于的DateTimePicker WPF + + +绑定接受通过userinput DD / MM / YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!