更新的值不会反映在WPF中的GUI中 [英] Updated value is not reflected in GUI in WPF

查看:67
本文介绍了更新的值不会反映在WPF中的GUI中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Tab应用程序,其中包含一个主窗口和相关选项卡,每个选项卡都有一个单独的View-Model。

每个选项卡逐个初始化,一旦应用程序正常打开然后我想要从第二个选项卡更新一些值,这些更改应该反映在First选项卡上。

所以我实现了与这些属性相关的INotifyPropertChanged事件,但它没有反映在First选项卡上。

似乎在更改第二个选项卡中的值时,DataContext中的某些问题没有正确更新。



我尝试了什么:



我尝试更新第一个选项卡DataContext,同时在第二个选项卡中更改值(属性的设置器)。我在调试中已经看到该属性已更新,但它没有反映在GUI中。



谢谢。



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.

<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" />

推荐答案

我看不到您的属性JoinDate中实现了INotifyPropertChanged。你必须做类似于这里的实现:

INotifyPropertyChanged接口(System.ComponentModel) [ ^ ]



相关样本:

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


这篇关于更新的值不会反映在WPF中的GUI中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆