0
votes

What would be best way to bind date in DatePicker to something like this:

      "startedAt": {
        "year": 2021,
        "month": 9,
        "day": 11
      }

In json it looks like this, but these will be three separate ints in c# properties. My entry class that I use for my observable collection and Date class:

public class ListEntry : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public int? id { get; set; }
        public Media media { get; set; }
        public int? _score;

        public int? score
        {
            get => _score;
            set
            {
                if (value == _score)
                    return;

                _score = value;
                OnPropertyChanged();
            }
        }

        public int _Progress;
        public int progress
        {
            get => _Progress;
            set
            {
                if (value == _Progress)
                    return;

                _Progress = value;
                OnPropertyChanged();
            }
        }
        public string status { get; set; }
        public Date startedAt { get; set; }
    }

    public class Date : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string name = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }

        public int? _year;

        public int? year
        {
            get => _year;
            set
            {
                if (value == _year)
                    return;

                _year = value;
                OnPropertyChanged();
            }
        }

        public int? _month;

        public int? month
        {
            get => _month;
            set
            {
                if (value == _month)
                    return;

                _month = value;
                OnPropertyChanged();
            }
        }
        public int? _day;
        public int? day
        {
            get => _day;
            set
            {
                if (value == _day)
                    return;

                _day = value;
                OnPropertyChanged();
            }
        }
    }

What I need to achieve is when I change date in DatePicker these three properties will change too.

I've tried something like this on my own

<DatePicker>
                            <DatePicker.Date>
                                <MultiBinding Mode="TwoWay" StringFormat="{}{0}/{1}/{2}">
                                    <Binding Path="startedAt.month" TargetNullValue="2" />
                                    <Binding Path="startedAt.day" TargetNullValue="3" />
                                    <Binding Path="startedAt.year" TargetNullValue="2000" />
                                </MultiBinding>
                            </DatePicker.Date>
                        </DatePicker>

It picks up dates correctly, but changing one doesn't change properties and I'm getting errors. And it seems buggy, When i scroll down the page and go back to the top it will go back to the original binding instead of my date selected. It's not gonna work or I've done it wrong?

[0:] MultiBinding: '/20/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/20/1998' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/16/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/1/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/20/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/23/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '/20/2021' cannot be converted to type 'System.DateTime'
[0:] MultiBinding: '//' cannot be converted to type 'System.DateTime'

Perhaps I need converter of some sort and I can't really change classes, it might not deserialize i think?

it would be much simpler to just have a DateTime property in your model. DateTime has properties for Month/Day/Year, so is it really necessary to declare your own Date type? - Jason