将 WPF Datepicker 设置为 Null 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23121933/
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
Problems setting WPF Datepicker to Null
提问by Bitfiddler
I'm using WPF and MVVM. I have a property in my model that is a nullable Datetime (Datime?).
我正在使用 WPF 和 MVVM。我的模型中有一个属性是可为空的日期时间(日期时间?)。
My Xaml looks like:
我的 Xaml 看起来像:
<CheckBox Grid.Row="0" Grid.Column="2" x:Name="ChkBoxFromDate"
Margin="10,0,0,5" FontWeight="Bold">
Orders with Trades After Date:
</CheckBox>
<DatePicker Grid.Row="1" Grid.Column="2" Margin="10,0,0,0"
SelectedDate="{Binding FromDate}" HorizontalContentAlignment="Stretch">
<DatePicker.Style>
<Style TargetType="{x:Type DatePicker}">
<Setter Property="SelectedDate" Value="{Binding Path=FromDate, Mode=TwoWay}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ChkBoxFromDate, Path=IsChecked}" Value="false">
<Setter Property="SelectedDate" Value="{x:Null}" />
<Setter Property="IsEnabled" Value="false" />
</DataTrigger>
</Style.Triggers>
</Style>
</DatePicker.Style>
</DatePicker>
I have the same pattern of disabling and clearing via checkbox on a textbox and cobmobox and it works just fine. With Datepicker, the setting of the Null for selecteddate does not work. Neither the UI nor the model gets updated. IsEnabled gets set and the control greys out, but the value in the datepicker remains. What am I missing?
我通过文本框和 cobmobox 上的复选框禁用和清除相同的模式,它工作得很好。使用 Datepicker,selecteddate 的 Null 设置不起作用。UI 和模型都不会更新。IsEnabled 被设置并且控件变灰,但日期选择器中的值仍然存在。我错过了什么?
Update
更新
So apparently this is a deeper issue than I initially thought. What I thought was working on the textbox and combobox was not if fact working (the UI only made it look that way). What happens with the above code is that you are in fact clearing the binding by setting it to Null. I was hoping that it would be setting the value of the binding, but this is not the case. When you use the setter you are in fact clearing the binding and setting the property to null which is why the model does not get updated. So why does the code below work and why did the textbox and combobox 'appear' to clear themselves and the datepicker does not. As far as I can tell, the datepicker 'textbox' is not directly connected to the SelectedDate property. This is how it can display "Select a date" when it's null or empty and display a date otherwise. In other words there is some magic happening between setting the SelectedDate and displaying the text in the textbox. This is what allows the 'hack' to work in the xaml below. By setting the text (instead of SelectedDate) I am not clearing the binding on SelectedDate, but rather exercising the magic that the Control does to synchronize the Text property with the SelectedDate property. You can do the same trick for Combobox. For textbox however, the property you want to bind IS the text property and there does not appear to be a way to get around this. My work around for this control ended up being adding a command to the checkbox which sets the textbox binding value to null from the Model end of things and a binding to the IsChecked property. If anyone has any better ideas, please post below.
所以显然这是一个比我最初想象的更深层次的问题。我认为在文本框和组合框上工作的内容并不是事实(UI 只是让它看起来那样)。上面代码的情况是,您实际上是通过将绑定设置为 Null 来清除绑定。我希望它会设置绑定的值,但事实并非如此。当您使用 setter 时,您实际上是在清除绑定并将属性设置为 null,这就是模型不会更新的原因。那么为什么下面的代码可以工作,为什么文本框和组合框“似乎”可以清除自己而日期选择器没有。据我所知,日期选择器“文本框”没有直接连接到 SelectedDate 属性。这是它如何显示“选择日期”时 s 为 null 或为空,否则显示日期。换句话说,在设置 SelectedDate 和在文本框中显示文本之间发生了一些神奇的事情。这就是允许“hack”在下面的 xaml 中工作的原因。通过设置文本(而不是 SelectedDate),我并没有清除 SelectedDate 上的绑定,而是使用 Control 将 Text 属性与 SelectedDate 属性同步的魔法。您可以对 Combobox 执行相同的技巧。但是,对于文本框,您要绑定的属性是文本属性,似乎没有办法解决这个问题。我对这个控件的解决最终是向复选框添加一个命令,该复选框将文本框绑定值从模型端设置为 null,并将绑定到 IsChecked 属性。如果有人有更好的想法,
回答by Bitfiddler
Okay, so I noticed some other posts were talking about setting the "Text" of a datepicker when talking about clearing the control from codebehind. In a Hail Mary attempt I tried:
好的,所以我注意到其他一些帖子在谈论从代码隐藏中清除控件时正在谈论设置日期选择器的“文本”。在一次冰雹玛丽尝试中,我尝试过:
<Setter Property="Text" Value="{x:Null}" />
Instead of the "SelectedDate" Property and it works as intended. I replaces the text back to "Select a date" and sets the SelectedDate to null. Counterintuitive, but it works.
而不是“SelectedDate”属性,它按预期工作。我将文本替换回“选择日期”并将 SelectedDate 设置为空。违反直觉,但它有效。

