wpf - 正在填充文本框,但日期选择器不是通过 mvvm 绑定(bind)

标签 wpf mvvm datepicker propertychanged

我有一个带有日期的文本框,我想将其转换为日期选择器以进行双向绑定(bind)。但不幸的是,日期选择器没有显示日期。

                    <TextBox Grid.Row="1"
                         Grid.Column="4"
                         IsReadOnly="False"
                         Text="{Binding Path=BirthDate,
                                        Mode=OneWay,
                                        StringFormat='dd/MM/yyyy'}" />

以下代码不起作用, selecteddate 显示为空...(虽然其中有一个值)
                <DatePicker Grid.Row="1"
                           Grid.Column="4" FirstDayOfWeek="Monday" SelectedDate="{Binding BirthDate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ></DatePicker>

在应用程序的另一个位置,我还有一个可以工作的日期选择器:
            <DatePicker x:Name="DatePicker" 
                    Grid.Row="3"
                    Grid.Column="1"
                    FirstDayOfWeek="Monday"
                    SelectedDate="{Binding Parameter.PAR_Date,
                                           Mode=TwoWay}"  />

我看不出我做错了什么,看起来很简单。

我可以在日期选择器中输入一个日期,这很有效......非常奇怪......任何想法都会受到欢迎......

编辑:

通过使用 snoop 并查看 datepicker 上的绑定(bind)更进一步,它似乎有一个错误,我似乎有不同的预期类型:
System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'EVD.Framework.NullableDateTime' and 'System.Nullable`1[System.DateTime]'. Consider using Converter property of Binding. BindingExpression:Path=Birthdate; DataItem='SomeModel' (HashCode=31671132); target element is 'DatePicker' (Name=''); target property is 'SelectedDate' (type 'Nullable`1')
System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='04/10/1929' BindingExpression:Path=Birthdate; DataItem='SomeModel' (HashCode=31671132); target element is 'DatePicker' (Name=''); target property is 'SelectedDate' (type 'Nullable`1')

编辑2:

就是这样,在代码中日期时间是 NullableDateTime。感谢每个提出想法的人,尤其是 Viv。为每个人投票给每个人一个体面的建议:-)

最佳答案

这篇文章很好地解释了 StringFormat:
http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/

您需要记住使用 {0} 格式化时。

关于wpf - 正在填充文本框,但日期选择器不是通过 mvvm 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22614197/

相关文章:

wpf - 设计从外部控件 : How to have some sample data in designer but use inherited DC at runtime? 获取其 DataContext 的 WPF UserControl

c# - 如何在 XAML 中访问枚举值

c# - 了解 MVVM - 如何绑定(bind)数据 View ↔ ViewModel + catch 在 View 上按下键并在 viewModel 中启动函数

c# - Xamarin 窗体可绑定(bind)网格

jquery - Onclick 事件仅适用于双击

wpf - 在 XAML 中填充自定义数组属性

wpf - 如何从 png 图像中获取图标?

wpf - 加载 View 后执行命令WPF MVVM

JQuery UI 日期选择器 parseDate 'Missing number at position 6'

javascript - jQuery 日期选择器验证失败