C# 使用INotifyPropertyChanged绑定到属性的XAML仅在应用程序启动时有效
我在XAML中设置绑定的数据模型中有以下代码。问题是初始值被正确地加载到XAML中,但是没有反映任何更改。这是我第一次使用INotify,所以我有点迷路了C# 使用INotifyPropertyChanged绑定到属性的XAML仅在应用程序启动时有效,c#,xaml,windows-phone-8,data-binding,inotifypropertychanged,C#,Xaml,Windows Phone 8,Data Binding,Inotifypropertychanged,我在XAML中设置绑定的数据模型中有以下代码。问题是初始值被正确地加载到XAML中,但是没有反映任何更改。这是我第一次使用INotify,所以我有点迷路了 public class MainViewModel : INotifyPropertyChanged { public MainViewModel() { } public event PropertyChangedEventHandler PropertyChanged; private voi
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
if (propertyName=="BasicInfoText")
{
AdvancedInfoText = "Basic info updated";
}
if (propertyName=="AdvancedInfoText")
{
//do nothing
}
}
//text for the basic info
public String BasicInfoText
{
get { return _basicInfoText; }
set
{
if (_basicInfoText != value)
{
_basicInfoText = value;
NotifyPropertyChanged("BasicInfoText");
}
}
}
private String _basicInfoText = "Initial text";
//text for the advanced info
public String AdvancedInfoText
{
get { return _advancedInfoText; }
set
{
if (_advancedInfoText != value)
{
_advancedInfoText = value;
NotifyPropertyChanged("AdvancedInfoText");
}
}
}
private String _advancedInfoText = "Initial Advance Text";
我还尝试将私有的BasicInfo文本和advancedInfoText更改为公共文本,以确保更改会得到通知,但运气不佳。有什么建议吗
更新>
因此,我已尝试为这些活动注册,但仍然不起作用。代码现在如下所示:
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
this.PropertyChanged += MyViewModel_PropertyChanged;
}
//***********************************
//catching events
void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "BasicSelected":
{// Do something
BasicInfoText = "basic text";
break;
}
case "WideSelected":
{// Do something
BasicInfoText = "wide text";
break;
}
case "NarrowSelected":
{// Do something
BasicInfoText = "narrow text";
break;
}
case "ArtificalSelected":
{// Do something
BasicInfoText = "artificial text";
break;
}
}
}
//***********************************
//register handlers
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
protected void OnPropertyChanged(String propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
//properties for the calculation and behavior
public Boolean BasicSelected
{
get { return _basicSelected; }
set {
if (_basicSelected != value)
{
_basicSelected = value;
OnPropertyChanged("BasicSelected");
}
}
}
public Boolean _basicSelected = true;
//properties for the calculation and behavior
public Boolean WideSelected
{
get { return _wideSelected; }
set
{
if (_wideSelected != value)
{
_wideSelected = value;
OnPropertyChanged("WideSelected");
}
}
}
public Boolean _wideSelected = false;
//properties for the calculation and behavior
public Boolean NarrowSelected
{
get { return _narrowSelected; }
set
{
if (_narrowSelected != value)
{
_narrowSelected = value;
OnPropertyChanged("NarrowSelected");
}
}
}
public Boolean _narrowSelected = false;
//properties for the behavior test
public Boolean ArtificalSelected
{
get { return _artificalSelected; }
set
{
if (_artificalSelected != value)
{
_artificalSelected = value;
OnPropertyChanged("ArtificalSelected");
}
}
}
public Boolean _artificalSelected = false;
//text for the basic info
public String BasicInfoText
{
get { return _basicInfoText; }
set
{
if (_basicInfoText != value)
{
_basicInfoText = value;
OnPropertyChanged("BasicInfoText");
}
}
}
public String _basicInfoText = "Initial text 2";
//text for the advanced info
public String AdvancedInfoText
{
get { return _advancedInfoText; }
set
{
if (_advancedInfoText != value)
{
_advancedInfoText = value;
OnPropertyChanged("AdvancedInfoText");
}
}
}
public String _advancedInfoText = "Initial Advance Text 2";
XAML如下所示:
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
this.PropertyChanged += MyViewModel_PropertyChanged;
}
//***********************************
//catching events
void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "BasicSelected":
{// Do something
BasicInfoText = "basic text";
break;
}
case "WideSelected":
{// Do something
BasicInfoText = "wide text";
break;
}
case "NarrowSelected":
{// Do something
BasicInfoText = "narrow text";
break;
}
case "ArtificalSelected":
{// Do something
BasicInfoText = "artificial text";
break;
}
}
}
//***********************************
//register handlers
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, e);
}
}
protected void OnPropertyChanged(String propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
//properties for the calculation and behavior
public Boolean BasicSelected
{
get { return _basicSelected; }
set {
if (_basicSelected != value)
{
_basicSelected = value;
OnPropertyChanged("BasicSelected");
}
}
}
public Boolean _basicSelected = true;
//properties for the calculation and behavior
public Boolean WideSelected
{
get { return _wideSelected; }
set
{
if (_wideSelected != value)
{
_wideSelected = value;
OnPropertyChanged("WideSelected");
}
}
}
public Boolean _wideSelected = false;
//properties for the calculation and behavior
public Boolean NarrowSelected
{
get { return _narrowSelected; }
set
{
if (_narrowSelected != value)
{
_narrowSelected = value;
OnPropertyChanged("NarrowSelected");
}
}
}
public Boolean _narrowSelected = false;
//properties for the behavior test
public Boolean ArtificalSelected
{
get { return _artificalSelected; }
set
{
if (_artificalSelected != value)
{
_artificalSelected = value;
OnPropertyChanged("ArtificalSelected");
}
}
}
public Boolean _artificalSelected = false;
//text for the basic info
public String BasicInfoText
{
get { return _basicInfoText; }
set
{
if (_basicInfoText != value)
{
_basicInfoText = value;
OnPropertyChanged("BasicInfoText");
}
}
}
public String _basicInfoText = "Initial text 2";
//text for the advanced info
public String AdvancedInfoText
{
get { return _advancedInfoText; }
set
{
if (_advancedInfoText != value)
{
_advancedInfoText = value;
OnPropertyChanged("AdvancedInfoText");
}
}
}
public String _advancedInfoText = "Initial Advance Text 2";
`
向我们展示您的XAML代码您是否在某个地方更改了您的属性?项目作为INotifyPropertyChanged.PropertyChanged+=新的PropertyChangedEventHandleritem\u PropertyChanged;