在ViewModel中获取TreeView SelectedItem

时间:2017-04-12 02:58:28

标签: c# wpf xaml mvvm treeview

我正在尝试在ViewModel中选择TreeView Node

Xaml内部

<Grid>
        <TreeView ItemsSource="{Binding TreeViewModel.TreeData}">
            <TreeView.Resources>
                <HierarchicalDataTemplate ItemsSource="{Binding  GroupTables}" DataType="{x:Type tbl:StaticTablesGroup}">
                    <Label Content="{Binding Name}"/>

                </HierarchicalDataTemplate>

                <DataTemplate DataType="{x:Type tbl:GroupTable}">
                    <Label Content="{Binding Name}"/>
                </DataTemplate>
            </TreeView.Resources>

            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="FontWeight" Value="Bold" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>        
    </Grid>

在VieModel内部

public class TreeTablesViewModel : ViewModelBase
    {
        private TablesXML _tables;
        public TreeTablesViewModel()
        {

            _tables = Deserialize.GuymasterXml();
        }

        public List<StaticTablesGroup> TreeData
        {
            get
            {
                return _tables.StaticTablesGroups;
            }

            set
            {
                _tables.StaticTablesGroups = value;
                OnPropertyChanged("TreeData");
            }
        }       
    }

最后是数据类

[XmlRoot("Tables")]
    public class TablesXML
    {
        public TablesXML()
        {
            StaticTablesGroups = new List<StaticTablesGroup>();
        }


        [XmlArray("StaticGroups")]
        [XmlArrayItem("StaticGroup", typeof(StaticTablesGroup))]
        public List<StaticTablesGroup> StaticTablesGroups { get; set; }

    }

    public class StaticTablesGroup
    {
        public StaticTablesGroup()
        {
            GroupTables = new List<GroupTable>();
        }

        [XmlAttribute("Name")]
        public string Name { get; set; }

        //[XmlArray("StaticGroup")]
        [XmlElement("Table", typeof(GroupTable))]
        public List<GroupTable> GroupTables { get; set; }


    }


    public class GroupTable
    {

        [XmlElement("TableName")]
        public string Name { get; set; }
        [XmlElement("TableTag")]
        public string Tag { get; set; }

        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (_isSelected != value)
                {
                    _isSelected = value;

                }
            }
        }
    }

选择Treeview节点后,在GroupTable或StaticTablesGroup内引发IsSelected。 如何在TreeTablesViewModel中获取所选节点?

谢谢

2 个答案:

答案 0 :(得分:1)

您不应该尝试在视图模型中操作视图对象(TreeView,TreeViewItem,TreeNode等)。您查看模型必须仅操作business / poco对象。

如果要检索与所选可视对象(TreeViewItem或节点)对应的基础业务对象,可以使用如下行为:

public class TreeviewSelectedItemTracker
{
    public static TreeTablesViewModel GetSelectedItemHolder(DependencyObject obj)
    {
        return (TreeTablesViewModel)obj.GetValue(SelectedItemHolderProperty);
    }

    public static void SetSelectedItemHolder(DependencyObject obj, TreeTablesViewModel value)
    {
        obj.SetValue(SelectedItemHolderProperty, value);
    }

    public static readonly DependencyProperty SelectedItemHolderProperty =
        DependencyProperty.RegisterAttached("SelectedItemHolder", typeof(TreeTablesViewModel), typeof(TreeviewSelectedItemTracker), new PropertyMetadata(null, OnSelectedChanged));

    private static void OnSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        TreeView tv = (TreeView)d;
        tv.Loaded += Tv_Loaded;
    }

    private static void Tv_Loaded(object sender, RoutedEventArgs e)
    {
        TreeView tv = (TreeView)sender;
        tv.Loaded -= Tv_Loaded;
        tv.Unloaded += Tv_Unloaded;
        tv.SelectedItemChanged += Tv_SelectedItemChanged;
    }

    private static void Tv_Unloaded(object sender, RoutedEventArgs e)
    {
        TreeView tv = (TreeView)sender;

        tv.Unloaded -= Tv_Unloaded;
        tv.SelectedItemChanged -= Tv_SelectedItemChanged;
    }

    private static void Tv_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        TreeView tv = (TreeView)sender;

        var cotr = GetSelectedItemHolder(tv);

        cotr.SelectedItem = tv.SelectedItem;

    }
}

TreeView.SelectedItem直接返回与所选TreeviewItem相关联的业务对象。

在xaml中,使用附加属性将SelectedItemHolder绑定到视图模型

<TreeView ItemsSource="{Binding TreeViewModel.TreeData}" local:TreeviewSelectedItemTracker.SelectedItemHolder="{Binding}"/>

答案 1 :(得分:0)

获取TreeViewItem(我认为你的意思)的最简单方法是从树的选择处理程序中的项容器生成器中获取它。

    private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        TreeView tv = e.Source as TreeView;
        TreeViewItem tvi = tv.ItemContainerGenerator.ContainerFromItem(e.NewValue) as TreeViewItem;
    }

XAML

    <TreeView ItemsSource="{Binding TreeViewModel.TreeData}" SelectedItemChanged="treeView_SelectedItemChanged" >

从这里开始,我认为你可以进入视图模型。