1

(WInUI3) DatePicker SelectedDate x:Bind and Binding are different results ?? x:Bind => SelectedDate is null Binding => SelectedDate is 1923-1-1

Result Image :

result-screenshoot

How to do let binding is also null ?

Code :

<DatePicker SelectedDate="{x:Bind vm.MyDate, Mode=TwoWay}"/>
<StackPanel DataContext="{x:Bind vm}">
   <DatePicker SelectedDate="{Binding MyDate}"/>
</StackPanel>
public class MyTable : ObservableObject
{
    private DateTimeOffset? _mydate;
    public DateTimeOffset? MyDate
    {
        get => _mydate;
        set => SetProperty(ref _mydate, value);
    }
}
public sealed partial class MainWindow : Window
{
    MyTable vm = new MyTable();
    public MainWindow() { this.InitializeComponent(); }
}

1 Answer 1

2

It seems that Binding doesn't pass the null value and converts it to some default value. This might be because DateTimeOffset is a struct.

Anyhow, here's a workaround creating a custom control:

public class CustomDatePicker : DatePicker
{
    public CustomDatePicker()
    {
        this.RegisterPropertyChangedCallback(DatePicker.SelectedDateProperty, OnSelectedDatePropertyChanged);
    }

    private void OnSelectedDatePropertyChanged(DependencyObject sender, DependencyProperty dp)
    {
        if (this.SelectedDate <= this.MinYear)
        {
            this.SelectedDate = null;
        }
    }
}

It works with both Binding and x:Bind:

<local:CustomDatePicker SelectedDate="{x:Bind ViewModel.MyDate, Mode=TwoWay}" />
<StackPanel DataContext="{x:Bind ViewModel}">
    <local:CustomDatePicker SelectedDate="{Binding MyDate, Mode=TwoWay}" />
</StackPanel>
Sign up to request clarification or add additional context in comments.

1 Comment

Did you get to solve your problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.