Problems setting WPF Datepicker to Null

Bitfiddler

I'm using WPF and MVVM. I have a property in my model that is a nullable Datetime (Datime?).

My Xaml looks like:

<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?

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.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related