English 中文(简体)
Multiple Multiple树多级
原标题:Binding Multiple Classes to a WPF Treeview

我的树木观对Model级有正确的约束力,但我会有一些带孩子的“东京会议”的班轮,我不得不获得我的过急模板,以正确开展工作。

预 收

页: 1

<TreeView.Resources>
    <!-- HierarchicalDataTemplate for ClassModel -->
    <HierarchicalDataTemplate DataType= "{x:Type models:ClassModel}" ItemsSource = "{Binding Path=ChildClasses}">
        <TextBlock Text="{Binding Path=ClassName}"/>
    </HierarchicalDataTemplate>
    <DataTemplate DataType="{x:Type models:CADModel}">
        <TextBlock Text="{Binding Path=PartNumber}"/>
    </DataTemplate>
</TreeView.Resources>
public interface IClassModel : ITreeNode
{
    ObservableCollection<ClassModel> ChildClasses { get; set; }
    ObservableCollection<CADModel>? CADModels { get; set; }
    int ClassId { get; set; }
    int ParentClassId { get; set; }
    string ClassName { get; set; }
    bool HasChildClass { get; set; }
    LibraryModel? ParentLibrary { get; set; }
}

public class ClassModel : TreeNode, IClassModel
{
    public LibraryModel? ParentLibrary { get; set; } = null;
    public int ParentClassId { get; set; } = 0;
    public int ClassId { get; set; } = 0;
    public string ClassName { get; set; } = string.Empty;
    public bool HasChildClass { get; set; } = false;

    public ObservableCollection<ClassModel> ChildClasses { get; set; } = new ObservableCollection<ClassModel>();
    public ObservableCollection<CADModel>? CADModels { get; set; } = null;

    public ClassModel()
    {
        if (HasChildClass == false)
        {
            CADModels = new ObservableCollection<CADModel>()
            {
                new CADModel()
                {
                    Id = 0,
                    PartNumber = "Bob",
                    Revision = "A.1"
                }
            };
        }
    }
}
public interface ICADModel : ITreeNode
{
    string PartNumber { get; set; }
    string Revision { get; set; }
}
public class CADModel :TreeNode, ICADModel
{
    public string PartNumber { get; set; } = string.Empty;
    public string Revision { get; set; } = string.Empty;
}

“entergraph

I was expecting that i would see one CADmodel leaf called "Bob" below every ClassModel node

问题回答

AHierarchicalDataTemplate 只能对其<代码>儿童进行单独收集。

因此,在您的<代码>中,添加了新的计算财产。

public List<TreeNode> AllChildren => ChildClasses.Cast<TreeNode>().Union(CADModels).ToList();

and change the binding for ItemsSource in your HierarchicalDataTemplate to reference this new AllChildren property instead.

虽然AllChildren list is only a Collection of TreeNode items,但树木观察控制将检查每个儿童项目的实际类型,并使用每个项目的指定数据模板。

It doesn t matter if a particular ClassModel can have either child classes or cad models, or both.

如果你真的在树苗展示后添加儿童课堂或卡片模型,那么,你将需要处理你可以观察的“Changed的活动,以更新这一“AllChildren”财产。 否则,取代可观察的Collection<>用清单和编号宣布的推进剂;相反。

此外,除非你有良好理由,否则,在只有一个项目执行的情况下,没有必要为每一种物体建立接口。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签