从ViewModel访问TreeView

时间:2013-07-02 20:00:14

标签: c# wpf mvvm

我有一个带有ContextMenu的TreeView,它包含添加/删除节点的选项。当我向树添加一个节点时,我正在向我的视图模型发送一个命令,该模型通过为显示树和一个后备数据对象添加一个包装来处理添加请求。当我这样做时,我希望显示树中新创建的包装器成为树视图的选定项。我怎样才能做到这一点?我不知道如何从viewmodel访问树视图,并怀疑你甚至可能不应该这样做。感谢。

2 个答案:

答案 0 :(得分:1)

在TreeViewItem的ViewModel中,您有一个“IsSelected”属性,如:

/// <summary>
/// Base class for all ViewModel classes displayed by TreeViewItems.  
/// This acts as an adapter between a raw data object and a TreeViewItem.
/// </summary>
public class TreeViewItemViewModel : CoreViewModel
{
    #region Data

    private readonly ObservableCollection<TreeViewItemViewModel> _children;
    private readonly TreeViewItemViewModel _parent;

    private bool _isExpanded = false;
    private bool _isSelected;

    #endregion // Data

    #region Constructors
    #endregion // Constructors

    #region Children

    /// <summary>
    /// Returns the logical child items of this object.
    /// </summary>
    public ObservableCollection<TreeViewItemViewModel> Children
    {
        get { return _children; }
    }

    #endregion // Children

    #region HasLoadedChildren
    #endregion // HasLoadedChildren

    #region IsExpanded

    /// <summary>
    /// Gets/sets whether the TreeViewItem 
    /// associated with this object is expanded.
    /// </summary>
    public bool IsExpanded
    {
        get { return _isExpanded; }
        set
        {
            if (value != _isExpanded)
            {
                _isExpanded = value;
                OnPropertyChanged("IsExpanded");
            }

            // Expand all the way up to the root.
            if (_isExpanded && _parent != null)
                _parent.IsExpanded = true;

            // Lazy load the child items, if necessary.
            if (this.HasDummyChild)
            {
                this.Children.Remove(DummyChild);
                this.LoadChildren();
            }

        }
    }

    #endregion // IsExpanded

    #region IsSelected

    /// <summary>
    /// Gets/sets whether the TreeViewItem 
    /// associated with this object is selected.
    /// </summary>
    public bool IsSelected
    {
        get { return _isSelected; }
        set
        {
            if (value != _isSelected)
            {
                _isSelected = value;
                OnPropertyChanged("IsSelected");
            }
        }
    }

    #endregion // IsSelected

然后在您的XAML中,您需要为TreeViewItem设置样式:

<TreeView.Resources>
                <Style x:Key="TreeViewItemStyle" TargetType="{x:Type TreeViewItem}">
                    <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                </Style>
</TreeView.Resources>

现在,您可以通过在新添加的项目中将IsSelected属性设置为True来设置TreeView的SelectedItem。

答案 1 :(得分:-1)

如果我了解你,你需要绑定到SelectedItem。 所以在你的ViewModel中创建:

private your_object_name _selectedItem;
public your_object_name SelectedItem 
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        RaisePropertyChanged("SelectedItem");
    }
}

在TreeView控件的XAML代码中:

 <TreeView SelectedItem="{Binding SelectedItem, Mode=TwoWay}" .../>

Mode=TwoWay允许您使用ViewModel中的SelectedItem属性更改TreeView控件上的Selection。

最好的问候。