我知道关于这个主题有上千个问题,但我找不到我的问题出在哪里......
我有一个. xaml.cs文件,它有标准的notify属性changed stuff,一个公共成员,在setter中有"notify",我还在构造函数中设置了数据上下文,如下所示:
public partial class SlimERDplot : UserControl
{
public SlimERDplot()
{
DataContext = this;
InitializeComponent();
}
public enum AvailableImaginations { NotSet, Right, Left}
private AvailableImaginations _movementImagination = AvailableImaginations.NotSet;
//This is the one I'll be binding
public string HemisphereUI { get { return Hemisphere.ToString() + " hemisphere"; } }
public AvailableHemispheres Hemisphere
{
get { return _hemisphere; }
set
{
if (_hemisphere != value)
{
_hemisphere = value;
NotifyPropertyChanged();
NotifyPropertyChanged("HemisphereUI"); //tried with or without this,
//makes no difference
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML简单地:
<Label HorizontalAlignment="Right" Content="{Binding Path=HemisphereUI}"></Label
然后,从使用者XAML中,我这样使用它:
<UIControls:SlimERDplot x:Name="ERDRightDown" Hemisphere="Right" MovementImagination="Left" />
但是在标签中,它总是说NotSet
(默认值),我调试了一下,这个东西确实用正确的值通过了setter,但是显示的值从来没有改变
少了什么?
2条答案
按热度按时间smdncfj31#
必须使用
DependencyProperty
这里有一个例子
代码隐藏
XAML语言
ee7vknir2#
少了什么?
您的
UserControl
没有实现INotifyPropertyChanged
:这是视图实际订阅
PropertyChanged
事件所必需的。