【发布时间】:2013-05-24 12:15:54
【问题描述】:
INotifyPropertyChanged 在以下 sn-p 中始终为 null
xaml:
<TextBlock
Name="tbkContent"
Text="{Binding Path= value,Mode=TwoWay}"
TextWrapping="Wrap"
FontSize="22"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextAlignment="Center"
Foreground="White"
Height="100"
Width="400" />
CS:
// ...
TextContent content = new TextContent();
// ...
public class TextContent:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private string _value;
public string value
{
get
{
return _value;
}
set
{
_value = value;
this.NotifyPropertyChanged("value");
}
}
}
// ...
private void BindingImages(int ImgId)
{
switch (ImgId)
{
case 1:
content.value = "binding1";
break;
case 2:
content.value = "binding2";
break;
case 3:
content.value = "binding3";
break;
default:
content.value = "binding4";
_incrID = 0;
break;
}
DoTransitions(ImgBg);
}
永远不会分配 TextblockValue。这始终为空
if (PropertyChanged != null) // <-----null here
{
}
Textblock 的文本没有正确更新..
最后,我自己动手了,下面的已经成功了
开关(ImgId) {
case 1:
_textcontent = new TextContent();
_textcontent.content = "binding1";
tbkContent.DataContext = _textcontent;
break;
case 2:
_textcontent = new TextContent();
_textcontent.content = "binding2";
tbkContent.DataContext = _textcontent;
break;
case 3:
_textcontent = new TextContent();
_textcontent.content = "binding3";
tbkContent.DataContext = _textcontent;
break;
default:
_textcontent = new TextContent();
_textcontent.content = "binding4";
tbkContent.DataContext = _textcontent;
_incrID = 1;
break;
}
我错过了设置 datacontext 属性.. 谢谢大家
【问题讨论】:
-
问题是什么?
-
括号数量不匹配,代码缩进错误,您帖子的其他部分缺少格式。如果您愿意发布正确的代码并正确使用括号,我很乐意为您编辑。
-
@Jon-- 问题是值没有绑定..
-
你在哪里设置数据上下文?
-
不要使用
value作为属性名。value是 C# 关键字...
标签: silverlight windows-phone-7 mvvm binding inotifypropertychanged