C# 将具有不同层次结构的两个viewmodel绑定到一个模型
我有带实体的viewport3d和带实体层次结构的treeview。Treeview是从类层次结构绑定的,如下所示:C# 将具有不同层次结构的两个viewmodel绑定到一个模型,c#,wpf,mvvm,treeview,viewport3d,C#,Wpf,Mvvm,Treeview,Viewport3d,我有带实体的viewport3d和带实体层次结构的treeview。Treeview是从类层次结构绑定的,如下所示: public class SomeDataObject { private ObservableCollection<SomeDataObject> _children; private string _OtherProperty; public SomeDataObject() { this._children =
public class SomeDataObject
{
private ObservableCollection<SomeDataObject> _children;
private string _OtherProperty;
public SomeDataObject()
{
this._children = new ObservableCollection<SomeDataObject>();
//other initialization
}
public ObservableCollection<SomeDataObject> Children
{
get
{
return this._children;
}
set{ _children = value; }
}
public string OtherProperty
{
get
{
return this._OtherProperty;
}
set{ _OtherProperty = value; }
}
}
我希望只绑定到所有对象的viewport OtherProperty,而不绑定层次。
视口提供来自ObservableCollection的绑定,但不包括祖先。因此,您无权访问较低级别的OtherProperty
我考虑两个独立的viewmodel,但我不知道如何设计不同viewmodel之间的通信
我找到了解决方案:
当然,这是一种迂回的方式。
我想自动更新viewmodels和model中的更改
在这种情况下,最好的解决方案是什么
编辑:
这是treeview的XAML:
<TreeView Grid.Column="0" Name="MainTreeView">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type WpfTreeViewTricks:SomeDataObject}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="TreeViewItem:"/>
<TextBlock Margin="1,0,0,0" Text="{Binding Path=OtherProperty}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
这就是工作
问题在于将层次结构绑定到视口时,如果视口允许以下方式绑定:
<ht:HelixViewport3D ItemsSource="{Binding Objects}" Background="{ht:LinearGradientBrush Gray, White}"/>
其中,对象是DataContext的属性:
public ObservableCollection<Visual3D> Objects { get; set; }
您能给我们看一下xaml并告诉我们您想要绑定什么吗?另外,您真的想在构造函数中设置私有字段\u children还是在属性中设置?对不起,这只是一个示例,我写了更多,我希望这可以解释。