gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:28:27 26 4
gpt4 key购买 nike

我有一个带有日期的文本框,我想将其转换为日期选择器以进行双向绑定(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/

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