Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mageshsankar opened this issue Nov 30, 2020 · 9 comments
Open
Labels
area-Binding bug Something isn't working product-winui3 WinUI 3 issues team-Markup Issue for the Markup team

Comments

@mageshsankar
Copy link

mageshsankar commented Nov 30, 2020

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.DataContext>
<local:ViewModel />
</Page.DataContext>

<local:CustomPicker x:Name="customPicker" NullableDateTime="{Binding SelectedDateTime, UpdateSourceTrigger=PropertyChanged}" />

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));
        }
    }



    /// <summary>
    /// Raised when a property on this object has a new value.
    /// </summary>        
    public event PropertyChangedEventHandler PropertyChanged;




    /// <summary>
    /// Raises this object's PropertyChanged event.
    /// </summary>
    /// <param name="propertyName">The property that has a new value.</param>
    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));



    /// <summary>
    /// Occurs when the <see cref="NullableDateTime"/> property is changed.
    /// </summary>
    /// <param name="sender">The d<see cref="DependencyObject"/>.</param>
    /// <param name="args">The e<see cref="DependencyPropertyChangedEventArgs"/>.</param>
    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

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 30, 2020
@StephenLPeters
Copy link
Contributor

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

@StephenLPeters StephenLPeters added team-Markup Issue for the Markup team area-Binding needs-author-feedback Asked author to supply more information. and removed team-Markup Issue for the Markup team needs-triage Issue needs to be triaged by the area owners labels Dec 1, 2020
@mageshsankar
Copy link
Author

Hi @StephenLPeters

I have found the issue in WinUi 3

Thanks

@ghost ghost added needs-triage Issue needs to be triaged by the area owners and removed needs-author-feedback Asked author to supply more information. labels Dec 1, 2020
@ranjeshj ranjeshj added product-winui3 WinUI 3 issues and removed needs-triage Issue needs to be triaged by the area owners labels Dec 2, 2020
@stevenbrix
Copy link
Contributor

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

@stevenbrix stevenbrix added the team-Markup Issue for the Markup team label Dec 8, 2020
@mageshsankar
Copy link
Author

mageshsankar commented Dec 17, 2020

@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
Copy link
Author

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
Copy link
Author

Hi Team,

Any update on this?

@wbokkers
Copy link

wbokkers commented Mar 5, 2021

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
Copy link

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
Copy link

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

@duncanmacmichael duncanmacmichael added the bug Something isn't working label Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding bug Something isn't working product-winui3 WinUI 3 issues team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

8 participants