-1

i have tried to bind the 2 calendar with same property but the issue is when when we select other month day then which is not affecting in another calendar.

XAML:

<Calendar x:Name="Cal1"  Grid.Column="0" Grid.Row="0" SelectedDate="{Binding Selectdate, Mode=TwoWay}" SelectionMode="MultipleRange" RenderTransformOrigin="0.515,0.478" Margin="85,0,103,337" />

<Calendar x:Name="Cal2"  SelectedDate="{Binding Selectdate, Mode=TwoWay}" Width="192" Grid.Column="1" SelectionMode="MultipleRange" Grid.Row="0" Margin="94,0,94,337"/>

Property:

private DateTime _selectdate;

public DateTime Selectdate
{
    get { return _selectdate; }

    set
    {
        if (value != _selectdate)
        {
            _selectdate = value;
        }
    }
}
Martin
  • 16,093
  • 1
  • 29
  • 48
  • Your viewmodel class needs to implement INotifyPropertyChanged, and its `Selectdate` property needs to raise the PropertyChanged notification when its value changes. [Here's an example of how to implement INotifyPropertyChanged](https://stackoverflow.com/a/36151255/424129) in a viewmodel base class. Once you've got that base class, just inherit your viewmodel from it, and use the SetProperty() method in your property setters as shown in that answer. I think that's the only thing missing here. If you run into trouble, let me know. – 15ee8f99-57ff-4f92-890c-b56153 Oct 25 '19 at 14:36
  • Hi Thanks for your help, i have tried your suggestion but still its not working as per my requirement for example, i have 2 calendar named as cal1 and cal2 , if i am selecting a date in cal1 the same selection is happening in cal2 but if i am trying to change the month or year it is happening in cal1 not in cal2 . The selection operation is working only when the month displayed in the calendars as same. – Feroz Shaikh Oct 28 '19 at 07:05
  • Please show the new version of your code. – 15ee8f99-57ff-4f92-890c-b56153 Oct 28 '19 at 11:08

1 Answers1

1

As Ed Plunkett writes, to update the controls your class needs to implement INotifyPropertyChanged from System.ComponentModel. In the setters of your properties, raise the PropertyChanged event so that the controls know the value of the property has changed. For example:

using System;
using System.ComponentModel;

namespace Test
{
    class CalendarExample : INotifyPropertyChanged
    {
        private DateTime _selectdate;

        public DateTime Selectdate
        {
            get { return _selectdate; }

            set
            {
                if (value != _selectdate)
                {
                    _selectdate = value;
                    PropertyChanged(this,
                        new PropertyChangedEventArgs(nameof(Selectdate)));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged = delegate { };
    }
}

As Ed notes, if this is a large application, it may be worthwhile implementing a "View Model Base class" for convenience.