Click here to Skip to main content
15,881,092 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created one Tab application which contains One main window and related tab, each tab has a separate View-Model.
Each tab initialized one by one, Once the app is open properly then I want to update some values form the second tab and these changes should be reflected on the First tab.
So I have implemented INotifyPropertChanged event related to these properties but it's not reflected on First Tab.
It seems there is some problem in DataContext is not updated properly while changing the value in the second tab.

What I have tried:

I have tried to updated First tab DataContext while changing the values(Property's setter) form the Second tab. I have seen in debugging the property has updated but it's not reflected in GUI.

Thanks.

C#
<pre>//First tab initialization .
public partial class PersonUserControl : UserControl
    {
        /// <summary>
        ///     Default constructor.
        /// </summary>
        public PersonUserControl ()
        {
            InitializeComponent();

            // Allow for UserControl to init successfully within VS Designer.
            if ( !DesignerProperties.GetIsInDesignMode(this) )
            {
                DataContext = new PersonViewModel();
            }
        }

 //First tab View- Model: PersonViewModel.cs

 public string JoinDate
        {
            get
            {
                return _joinDate;
            }
            set
            {
                if (_joinDate != value)
                {
                    _joinDate = value;
                    OnPropertyChanged("JoinDate");
                }
            }
        }

//First Tab XAML component.

<pre> <TextBlock FontWeight="Bold" 
               Height="28" 
               HorizontalAlignment="Left" 
               Margin="30,350,0,0"
               Text="{Binding JoinDate, Mode=OneWay}" 
               VerticalAlignment="Top" />


//Second tab initialization.

public partial class UpdateInfoControl : UserControl
    {
        /// <summary>
        ///     Default constructor.
        /// </summary>
        public UpdateInfoControl()
        {
            InitializeComponent();

            // Allow for UserControl to init successfully within VS Designer.
            if ( !DesignerProperties.GetIsInDesignMode(this) )
            {
                DataContext = new UpdateInfoModel();
            }
        }

//Second tab View-Model: UpdateInfoModel.cs

public DateTime TimeOfDay
        {
            get
            {
                return _timeOfDay;
            }
            set
            {
		if ( _timeOfDay != value )
		  {
		          _timeOfDay = value;
		           OnPropertyChanged("TimeOfDay");
		
			_updateInfoViewModel.JoinDate = timeOfDay .ToString();
		}
		
             }
}

//Second Tab XAML component.

<DatePicker Height="26"
                          HorizontalAlignment="Right"
                          Margin="0,69,239,0"
                          SelectedDate="{Binding TimeOfDay, Mode=TwoWay}"
                          SelectedDateFormat="Long"
                          VerticalAlignment="Top"
                          Width="208" />
Posted
Updated 14-May-18 2:53am
v3
Comments
[no name] 14-May-18 3:27am    
Could you show us your code?
Vipin_Sharma_ 14-May-18 5:44am    
I have updated.
Kenneth Haugland 14-May-18 8:35am    
And the XAML code?
Vipin_Sharma_ 14-May-18 8:52am    
I have updated.
[no name] 14-May-18 23:37pm    
Why do you have "OneWay" on a TextBlock? A TextBlock can only be "one way".

Why are you converting dates to strings before displaying them?

Why don't you just bind both controls to the same source?

1 solution

I cannot see that the INotifyPropertChanged is implemented in your property JoinDate. You have to do something similar to the implementation here:
INotifyPropertyChanged Interface (System.ComponentModel)[^]

Relevant sample:
public class YourClass : INotifyPropertyChanged
   {

       public event PropertyChangedEventHandler PropertyChanged;
       private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
       {
           if (PropertyChanged != null)
           {
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
           }
       }

       public string JoinDate
       {
           get
           {
               return _joinDate;
           }

           set
           {
               if (value != _joinDate)
               {
                   _joinDate = value;
                   NotifyPropertyChanged();
               }
           }
       }
   }
 
Share this answer
 
Comments
Vipin_Sharma_ 14-May-18 9:00am    
I have done the same implementation of an INotifyPropertyChanged interface but it's not reflecting in the first Tab during changing the date from the second tab.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900