我有一个 TreeView,它从数据绑定的 ObservableCollections 创建其所有项目。我有一个 GameNode 对象的层次结构,每个对象都有两个 ObservableCollections。一个集合具有 EntityAttrib 对象,另一个具有 GameNode 对象。您可以说 GameNode 对象代表文件夹,而 EntityAttrib 代表文件。为了在同一个 TreeView 中同时显示属性和 GameNode,我使用了多重绑定。这一切在启动时都可以正常工作,但是当我在层次结构中的某处添加新的 GameNode 时,TreeView 不会更新。我在转换器方法中设置了一个断点,但在添加新的 GameNode 时没有调用它。似乎 ObservableCollection 没有通知 MultiBinding 更改。如果我注释掉 MultiBinding 并且只绑定 GameNode 集合,它会按预期工作。
XAML:
<HierarchicalDataTemplate DataType="{x:Type local:GameNode}">
<HierarchicalDataTemplate.ItemsSource>
<MultiBinding Converter="{StaticResource combineConverter}">
<Binding Path="Attributes" />
<Binding Path="ChildNodes" />
</MultiBinding>
</HierarchicalDataTemplate.ItemsSource>
<TextBlock Text="{Binding Path=Name}" ContextMenu="{StaticResource EntityCtxMenu}"/>
</HierarchicalDataTemplate>
C#:
public class GameNode
{
string mName;
public string Name { get { return mName; } set { mName = value; } }
GameNodeList mChildNodes = new GameNodeList();
public GameNodeList ChildNodes { get { return mChildNodes; } set { mChildNodes = value; } }
ObservableCollection<EntityAttrib> mAttributes = new ObservableCollection<EntityAttrib>();
public ObservableCollection<EntityAttrib> Attributes { get { return mAttributes; } set { mAttributes = value; } }
}
GameNodeList 是 ObservableCollection 的子类