Question

How do I do so that it is possible to expand/collaps groups in the TreeView simply by clicking on the text, instead of clicking the arrow to the left.

Was it helpful?

Solution

You should create style for your Tree Item with next setter:

        <Style x:Key="TreeItemStyle"
               TargetType="{x:Type TreeViewItem}">                
            <Setter Property="IsExpanded"
                    Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
        </Style>

Then add to you group view data class observable property named IsExpanded:

    private bool _isExpanded;

    public bool IsExpanded
    {
        get
        {
            return this._isExpanded;
        }
        set
        {
            if (this._isExpanded != value)
            {
                this._isExpanded = value;
                this.OnPropertyChanged("IsExpanded");
            }
        }
    }

Then intercept hyper link click event and set IsExpanded as true:

    private void Hyperlink_Click(object sender, RoutedEventArgs e)
    {
        var dc = ((Hyperlink)sender).DataContext;
        if (dc is GroupViewData)
        {
            ((GroupViewData)dc).IsExpanded = true;
        }
    }

Of course, the best way is to use commands instead of click handlers, but I don't know composition of your presentation model so can't provide proper solution. I just must say that in our projects with alike requirements we successfully avoid any view code behind. God bless WPF!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top