wpf mvvm treeview获取所选项目 [英] wpf mvvm treeview get selected item

查看:386
本文介绍了wpf mvvm treeview获取所选项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让我尝试以下代码,但是当我在树视图中选择一个项目时,IsSelected属性没有得到更新。请指导我如何解决这个问题。实际上我的要求是在树视图中选择一个项目时做一些功能。



To get I am trying the following code but it the IsSelected property is not getting updated when I select an item in the treeview. Please guide me how to resolve this. Actually my requirement is to do some functionality on selecting an item in the treeview.

<TreeView ItemsSource="{Binding TreeData}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>




public class TViewModel : INotifyPropertyChanged
{
    private static object _selectedItem = null;
    // This is public get-only here but you could implement a public setter which also selects the item.
    // Also this should be moved to an instance property on a VM for the whole tree, otherwise there will be conflicts for more than one tree.
    public static object SelectedItem
    {
        get { return _selectedItem; }
        private set
        {
            if (_selectedItem != value)
            {
                _selectedItem = value;
                OnSelectedItemChanged();
            }
        }
    }

    static virtual void OnSelectedItemChanged()
    {
        // Raise event / do other things
    }

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

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}





代码块修复[/ edit ]

推荐答案


Hello

我会通过创建一个继承TreeView的控件来解决这个问题,并且具有依赖属性的SelectedItem属性,因此可以在XAML代码和绑定中使用它。让我们调用控件MyTreeView。



注意:通过这样做,我不知道如果属性可以设置,但它肯定可以使用获取所选项目。



这里是控件的一些示例代码:

Hello
I would approach this by creating a control that inherits TreeView, and has the SelectedItem property with a dependency property so it can be used in XAML code and bindings. Let''s call the control MyTreeView.

NOTE: By doing this, I don''t kow if the property can be SET, but it can certainly be used to get the selected item.

Here''s some example code for the control:

public class MyTreeView : TreeView, INotifyPropertyChanged
{
    public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register("SelectedItem", typeof(Object), typeof(MyTreeView), new PropertyMetadata(null));
    public new Object SelectedItem
    {
        get { return (Object)GetValue(SelectedItemProperty); }
        set
        {
            SetValue(SelectedItemsProperty, value);
            NotifyPropertyChanged("SelectedItem");
        }
    }

    public MyTreeView()
        : base()
    {
        base.SelectedItemChanged += new RoutedPropertyChangedEventHandler<Object>(MyTreeView_SelectedItemChanged);
    }

    private void MyTreeView_SelectedItemChanged(Object sender, RoutedPropertyChangedEventArgs<Object> e)
    {
        this.SelectedItem = base.SelectedItem;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String aPropertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(aPropertyName));
    }
}





您现在可以将控件用作普通的TreeView控件,并进行更改。



You can now use your control as an ordinary TreeView control, with your alterations.

<grid>
    <local:mytreeview itemssource="{Binding Path=Items, Mode=OneWay}" xmlns:local="#unknown">
                      SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}"
                      Margin="0,0,0,20" />
    <textblock text="{Binding Path=SelectedItem, Mode=OneWay}">
               VerticalAlignment="Bottom" />
</textblock></local:mytreeview></grid>





希望它有所帮助!



Hope it helps!


您好,感谢您的回复。



我试过这个,我能够在依赖属性类中查看Selected项。但是我想在我视图的视图模型类中访问这个选定的项目。请让我知道如何做到这一点。



我试图通过将它绑定到viewmodel中的属性来实现它,但它不起作用。请让我知道如何做到这一点。







先谢谢。
Hi, Thanks for your reply.

I tried this and I am able to view the Selected item in the dependency property class. But I wan to access this selected item in my view''s view model class. Please let me know how to do this.

I tried to implement this by binding it to a property in the viewmodel but itsis not working. Please let me know how to do this.



Thanks in Advance.


Hi
模型类应该是视图的DataContext。



在视图类中,你可以在构造函数中通过代码

Hi The model class should be the view''s DataContext.

In the view class, you can set this either in the constructor by code"
this.DataContext = new ViewClass();





或者在XAML中,在起始标记中:



Or in the XAML, in the starting tag:

<viewmodel mode="hold" />                        DataContext="{local:ViewClass}"  />





现在,您可以绑定XAML。

希望它有所帮助。



Now, you can bind form the XAML.
Hope it helped.


这篇关于wpf mvvm treeview获取所选项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆