我有以下的xaml;
<DataTemplate DataType="{x:Type NameSpace:Node}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Item.Value}"/>
<ContentControl Content="{Binding Item}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type NameSpace:Item}">
<TextBlock Text="{Binding Value}" />
</DataTemplate>当我使用Node模板显示Node对象时,我得到两个TextBlocks,它们都显示相同的值。到目前一切尚好。当项目发生更改时,就会出现问题。当Node类触发INotifyPropertyChanged事件时,Node DataTemplate中的TextBlock按预期更新,但条目DataTemplate中的TextBlock不会更新。
当Node类触发IPropertyChanged事件时,如何使项IPropertyChanged更新其绑定?
上面的更新适用于以下简单的场景;
Xaml
<DataTemplate DataType="{x:Type DataTemplateExample:Node}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Item.Value}"/>
<ContentControl Content="{Binding Item}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type DataTemplateExample:Item}">
<TextBlock Text="{Binding Value}" />
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<ContentControl Content="{Binding MyNode}"/>
<Button Command="{Binding ChangeMyNodeItem}">Change Item</Button>
</StackPanel>
</Grid>
</Window>c#
public class MainViewModel
{
private readonly Node myNode;
private readonly DelegateCommand changeMyNodeItemCmd;
public MainViewModel()
{
myNode = new Node {Item = new Item("a")};
changeMyNodeItemCmd = new DelegateCommand(()=>
myNode.Item = new Item("b"));
}
public Node MyNode { get { return myNode; } }
public ICommand ChangeMyNodeItem
{
get { return changeMyNodeItemCmd; }
}
}
public class Node : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Item item;
public Item Item
{
set
{
item = value;
if (PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs("Item"));
}
get { return item; }
}
}
public class Item
{
private readonly string value;
public Item(string value)
{
this.value = value;
}
public string Value
{
get { return value; }
}
}在我的真实场景中,我使用的是代理,我认为这就是WPF被搞混的原因。物品实际上并没有改变--它正在被重新映射。
最终,我使用了与ShadeOfGray类似的解决方案解决了这个问题。但我要指出的是,除非你使用代理,否则这是不必要的。
发布于 2012-12-05 08:42:10
从你发布的信息来看,我认为你在错误的课堂上解雇了NotifyPropertyChanged。在您的场景中,类似的事情应该能正常工作。
根据评论更新:
public class Node : INotifyPropertyChanged
{
private Item item;
public event PropertyChangedEventHandler PropertyChanged;
public Item Item
{
get
{
return item;
}
set
{
item = value;
this.NotifyPropertyChanged("Item");
if (item != null)
{
item.ForcePropertyChanged("Value");
}
}
}
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Item : INotifyPropertyChanged
{
private string itemValue;
public Item()
{
this.Value = string.Empty;
}
public event PropertyChangedEventHandler PropertyChanged;
public string Value
{
get
{
return itemValue;
}
set
{
itemValue = value;
NotifyPropertyChanged("Value");
}
}
public void ForcePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}https://stackoverflow.com/questions/13718597
复制相似问题