的炫魅:为什么PropertyChangedEventHandler为空?
MainPage.xaml中
<Canvas x:Name="LayoutRoot" Background="White">
</Canvas>
MainPage.xaml.cs中
List<Usol> list = new List<Usol>();
for (int i = 0; i < 10; i++)
{
var element = new Usol();
list.Add(element);
Canvas.SetTop(element, i * 25);
LayoutRoot.Children.Add(list[i]);
}
foreach (var item in list)
{
item.context.name = "Varken";
}
一个用户控件
Usol.xaml
<Grid x:Name="LayoutRoot" Background="White">
<TextBlock Text="{Binding Name}" />
</Grid>
Usol.xaml.cs
public Context context;
public Usol()
{
InitializeComponent();
context = new Context();
this.DataContext = context;
}
一类
Context.cs
public class Context : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#region Fields
/// <summary>
/// Field Declaration for the <see cref="Name"/>
/// </summary>
private string name;
#endregion
#region Properties
/// <summary>
/// Gets or Sets the Name
/// </summary>
public string Name
{
get { return name; }
set
{
if (this.name != value)
{
this.name = value;
OnPropertyChanged("Name");
}
}
}
#endregion
}
现状
我创造了这个小测试应用程序在一个更大的应用程序来复制一个问题,我有。它以相同的方式工作(不完全,但足够接近)。
它增加了一些定制的用户控件和每个获得自己的一个DataContext类的实例。
然而,没有任何属性都愿意更新themselfs由于空PropertyChangedEventHandler
。
问题
为什么public event PropertyChangedEventHandler PropertyChanged;
总是空?
只是一个建议。您没有遵循预期的字段/属性名称约定。 –
你的意思是没有人订阅?如果您使用OneTime绑定,这是我期望的行为。 。如果你没有定义接口(例如事件在那里,但类定义它),也会发生这种情况。 –