WPF MVVM TreeView SelectedItem [英] WPF MVVM TreeView SelectedItem

查看:92
本文介绍了WPF MVVM TreeView SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不会那么困难. WPF中的TreeView不允许您设置SelectedItem,因为该属性为ReadOnly.我正在填充TreeView,甚至在数据绑定集合发生更改时也进行更新.

This cannot be this difficult. The TreeView in WPF doesn't allow you to set the SelectedItem, saying that the property is ReadOnly. I have the TreeView populating, even updating when it's databound collection changes.

我只需要知道选择了哪个项目.我正在使用MVVM,因此没有代码隐藏或变量可引用树视图. 这是我发现的唯一解决方案,但这显然是一个黑客,它在XAML中创建另一个元素,该元素使用ElementName绑定将自身设置为treeviews所选项目,然后您还必须绑定Viewmodel. 几个其他

I just need to know what item is selected. I am using MVVM, so there is no codebehind or variable to reference the treeview by. This is the only solution I have found, but it is an obvious hack, it creates another element in XAML that uses ElementName binding to set itself to the treeviews selected item, which you must then bind your Viewmodel too. Several other questions are asked about this, but no other working solutions are given.

我已经看到了这个问题,但是使用给出的答案给了我编译错误,由于某种原因,我无法将对混合sdk System.Windows.Interactivity的引用添加到我的项目中.它说未知的错误system.windows尚未预加载",我还没有想出如何解决这个问题的方法.

I have seen this question, but using the answer given gives me compile errors, for some reason I cannot add a reference to the blend sdk System.Windows.Interactivity to my project. It says "unknown error system.windows has not been preloaded" and I haven't yet figured out how to get past that.

对于奖励积分:为什么微软到底使该元素的SelectedItem属性为ReadOnly?

For Bonus Points: why the hell did Microsoft make this element's SelectedItem property ReadOnly?

推荐答案

您真的不需要直接处理SelectedItem属性,只需绑定

You should not really need to deal with the SelectedItem property directly, bind IsSelected to a property on your viewmodel and keep track of the selected item there.

草图:

<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)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这篇关于WPF MVVM TreeView SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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