如何在Treeview wpf中获取关联的父级

问题描述 投票:0回答:1

[我们尝试在树状视图中选择子元素时获取直接关联的父元素。父名称绑定了Textblock(名称为Parent)。如何在后端或Mvvm模式中获取父元素名称?

我们已经添加了Xaml和viewmodel代码。请参考

我们希望得到答复。

xaml:

<TreeView SelectedItemChanged="TreeView_SelectedItemChanged" ItemsSource="{Binding Model.ResourceList}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding AttributeModelList}">
             <TextBlock x:Name="Parent" Text="{Binding Path=ResourceName}"></TextBlock>
             <HierarchicalDataTemplate.ItemTemplate>
                  <DataTemplate>
                       <TextBlock Text="{Binding Path=ColumnName}"/>
                  </DataTemplate>
             </HierarchicalDataTemplate.ItemTemplate>
         </HierarchicalDataTemplate>
     </TreeView.ItemTemplate>
</TreeView>

ViewModel类:

public class MainWindowViewModel : NotifyChanged
    {
        private JsonCreationModel _model;

        public JsonCreationModel Model
        {
            get
            {
                return _model;
            }
            set
            {
                _model = value;
                RaisePropertyChanged("Model");
            }
        }

        public MainWindowViewModel()
        {
            Model = new JsonCreationModel();
            for (int i = 0; i < 5; i++)
            {
                ResourceModel resource = new ResourceModel();
                resource.ResourceName = "Test" + i;
                resource.AttributeModelList = new ObservableCollection<AttributeModel>() {
                new AttributeModel { ColumnName="Name"+i},
                    new AttributeModel{ColumnName="Age"+i}};                
                Model.ResourceList.Add(resource);
            }
        }
    }

    public class JsonCreationModel : NotifyChanged
    {
        private ObservableCollection<ResourceModel> _resourceList;

        public ObservableCollection<ResourceModel> ResourceList
        {
            get
            {
                return _resourceList;
            }
            set
            {
                _resourceList = value;
                RaisePropertyChanged("ResourceList");
            }
        }

        public JsonCreationModel()
        {
            ResourceList = new ObservableCollection<ResourceModel>();
        }
    }

    public class NotifyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
    }

    public class AttributeModel : NotifyChanged
    {
        private string _columnName;

        public string ColumnName
        {
            get
            {
                return _columnName;
            }
            set
            {
                _columnName = value;
                RaisePropertyChanged("ColumnName");
            }
        }
    }

    public class ResourceModel : NotifyChanged
    {
        private string _resourceName;
        private ObservableCollection<AttributeModel> _attributeModelList;

        public string ResourceName
        {
            get
            {
                return _resourceName;
            }
            set
            {
                _resourceName = value;
                RaisePropertyChanged("ResourceName");
            }
        }

        public ObservableCollection<AttributeModel> AttributeModelList
        {
            get
            {
                return _attributeModelList;
            }
            set
            {
                _attributeModelList = value;
                RaisePropertyChanged("AttributeModelList");
            }
        }

        public ResourceModel()
        {
            this.AttributeModelList = new ObservableCollection<AttributeModel>();
        }
    }
wpf treeview itemtemplate treeviewitem hierarchicaldatatemplate
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.