microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.34k stars 677 forks source link

Binding is not working for property which is of DateTimeOffset? Type #3721

Open mageshsankar opened 3 years ago

mageshsankar commented 3 years ago

Describe the bug I am trying to bind value for property of nullable DateTimeOffset type, but binding does not work. But, when I manually set the value for particular property in code behind, it works properly. Please find the Code Snippet for the same below:

Expected behavior Binding Should work as expected

XAML:

<Page x:Class="BindingIssue_sample.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:BindingIssue_sample" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

C#: public class ViewModel : INotifyPropertyChanged { private DateTimeOffset dateTime = new DateTimeOffset(new DateTime(2019, 1, 1)); public DateTimeOffset? SelectedDateTime { get { return dateTime; } set { dateTime = value.Value; this.RaisePropertyChanged(nameof(this.SelectedDateTime)); } } /// /// Raised when a property on this object has a new value. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Raises this object's PropertyChanged event. /// /// The property that has a new value. protected virtual void RaisePropertyChanged(string propertyName) { if (this.PropertyChanged != null) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } public class CustomPicker : Control { public DateTimeOffset? NullableDateTime { get { return (DateTimeOffset?)GetValue(NullableDateTimeProperty); } set { SetValue(NullableDateTimeProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty NullableDateTimeProperty = DependencyProperty.Register("NullableDateTime", typeof(DateTimeOffset?), typeof(CustomPicker), new PropertyMetadata(null, OnDateTimeChangedAsync)); /// /// Occurs when the property is changed. /// /// The d. /// The e. private static async void OnDateTimeChangedAsync(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var dialog = new MessageDialog("Nullable DateTime property value changed"); _ = await dialog.ShowAsync(); } } **Version Info** NuGet package version: Windows app type: | UWP | Win32 | | :--------------- | :--------------- | | Yes | | | Windows 10 version | Saw the problem? | | :--------------------------------- | :-------------------- | | Insider Build (xxxxx) | | | May 2020 Update (19041) | Yes | | November 2019 Update (18363) | | | May 2019 Update (18362) | | | October 2018 Update (17763) | | | April 2018 Update (17134) | | | Fall Creators Update (16299) | | | Creators Update (15063) | | | Device form factor | Saw the problem? | | :----------------- | :--------------- | | Desktop | Yes | | Xbox | | | Surface Hub | | | IoT | | **Additional context**
StephenLPeters commented 3 years ago

@mageshsankar did you find this issue using Winui3 or system xaml (winui2)?

mageshsankar commented 3 years ago

Hi @StephenLPeters

I have found the issue in WinUi 3

Thanks

stevenbrix commented 3 years ago

@mageshsankar does this work with x:Bind? And do you know if it's just DateTimeOffset or is it any nullable type?

mageshsankar commented 3 years ago

@stevenbrix issue occurs only for nullable type, as you suggested x:Bind is working properly for nullable DateTimeOffset ,

The same scenario(binding) working properly for nullable DateTime type.

Issue only with nullable DateTimeOffset type

mageshsankar commented 3 years ago

Hi Team,

Is there any workaround solution to resolve the binding issue in custom control(CustomPicker) itself instead of using x:bind in application level.

mageshsankar commented 3 years ago

Hi Team,

Any update on this?

wbokkers commented 3 years ago

I can't x:Bind the Date property of CalendarDatePicker to DateTimeOffset (nullable or not), without getting an exception in WinUI 3 Preview 4

tanothing commented 1 year ago

Hi Team, Any update on this issue? I had same issue with Nullable DateTime too. I cannot bind string to LowerValue on XAML. Please take a look at this issue. Thanks! [DefaultValue(typeof(DateTime), "")] public DateTime? LowerValue { get => _lowerValue; set => SetLowerValue(value, false); } image

sethuramkumar commented 1 year ago

Hi Team, Any updates on the above issue? Any workaround/fix instead of using x;Bind ?