WPF DatePicker 显示 DateTime.Today OR 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17164018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:01:24  来源:igfitidea点击:

WPF DatePicker show DateTime.Today OR Binding

wpfxamldata-bindingdatepicker

提问by Wim Ombelets

What is the best way to have a WPF DatePickershow a predefined DateTime(e.g. DateTime.Today) while still maintaining binding?

在保持绑定的同时让 WPFDatePicker显示预定义DateTime(例如DateTime.Today)的最佳方法是什么?

SelectedDate="{x:Static sys:DateTime.Today}"

SelectedDate="{x:Static sys:DateTime.Today}"

and

Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty, Mode=TwoWay}"

Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty, Mode=TwoWay}"

don't play well together. When a UserControlis loaded, I'd like it to display today's date, until a row is selected. Is there a way to make them get along?

一起玩不好。当UserControl被加载,我想它显示今天的日期,直到选中一行。有没有办法让他们和睦相处?

回答by Bas

Text="{Binding Path=MyPublicProperty.ADateTimeMemberProperty, 
       Mode=TwoWay, FallbackValue={x:Static sys:DateTime.Today}}"

Use FallbackValue to set a value when it cannot be retrieved from the binding.

当无法从绑定中检索值时,使用 FallbackValue 设置值。