tags:

views:

53

answers:

2

ViewModel has 2 field. Name, Childs

I need like this

1. When click on the root element, do 2 operation
first. expand yourself
second. select first child. If child element has childs, repeat 1.
otherwise do nothing

Only last child (leaf) selectable

A: 

What about to capture select item event on the treeview and expand the first child of the selected item? It seems easy to do.

Victor Marzo
+1  A: 

UPDATE
Figured out a much better way to do this. This will also account for changes in the ObservableCollection.

The Xaml can just look like this

<Window.Resources>
    <HierarchicalDataTemplate x:Key="Level1" 
                                ItemsSource="{Binding Path=Childs}">
        <TextBlock Text="{Binding Path=Name}"/>
    </HierarchicalDataTemplate>
</Window.Resources>
<TreeView ItemsSource="{Binding}"
          ...>
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
            <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

And then we handle the IsSelected Property in the Model/ViewModel instead.

public class MyViewModel : INotifyPropertyChanged
{
    private static MyViewModel s_lastSelectedTestItem = null;

    public MyViewModel(string name)
    {
        Name = name;
        m_isSelected = false;
        Childs = new ObservableCollection<MyViewModel>();
        Childs.CollectionChanged += new NotifyCollectionChangedEventHandler(TestItems_CollectionChanged);
    }

    void TestItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (IsSelected == true && Childs.Count > 0)
        {
            Childs[0].IsSelected = true;
            IsExpanded = true;
        }
    }

    public string Name
    {
        get;
        set;
    }
    public ObservableCollection<MyViewModel> Childs
    {
        get;
        set;
    }

    private bool m_isSelected;
    public bool IsSelected
    {
        get
        {
            return m_isSelected;
        }
        set
        {
            m_isSelected = value;
            if (m_isSelected == true)
            {
                if (s_lastSelectedTestItem != null)
                {
                    s_lastSelectedTestItem.IsSelected = false;
                }
                s_lastSelectedTestItem = this;
                if (Childs.Count > 0)
                {
                    IsExpanded = true;
                    Childs[0].IsSelected = true;
                    m_isSelected = false;
                }
            }
            OnPropertyChanged("IsSelected");
        }
    }

    private bool m_isExpaned;
    public bool IsExpanded
    {
        get
        {
            return m_isExpaned;
        }
        set
        {
            m_isExpaned = value;
            OnPropertyChanged("IsExpanded");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Meleak