1

How do I use a DelegateCommand in a TreeView to get the Expanded event?

Should I be using the DelegateCommand or is there another way?

Thanks

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
user55474
  • 537
  • 1
  • 8
  • 25

1 Answers1

1

Since you are mentioning Prism, I assume you have a controller or ViewModel attached to the view containing your TreeView...

That being the case, expose a boolean property IsExpanded

    private bool _isExpanded;
    public bool IsExpanded
    {
        get { return _isExpanded; }
        set
        {
            if (value != _isExpanded)
            {
                _isExpanded = value;
                RaisePropertyChanged("IsExpanded");
                //  Apply custom logic here...
            }
        }
    }

Now to hook this property up to the TreeView, you need to apply the following style in the TreeView's resources (or further up the Visual tree as appropriate)

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

NB: You can also use a similar technique to hook up the IsSelected property - also very useful!!

kiwipom
  • 7,639
  • 37
  • 37
  • Actually, in Prism that would look like: ... public property IsExpanded: Boolean; notify; ... No need to manually raise the PropertyChanged event with Prism. – Sebastian P.R. Gingter Dec 07 '09 at 07:38
  • 2
    That's incorrect. You still need to raise the PropertyChanged notifier. Prism does not change the behavior of WPF. – Anderson Imes Dec 07 '09 at 19:06
  • i added the propeties but it does not seem to work. Am i missing anything – user55474 Dec 09 '09 at 02:02
  • It's difficult to guess what you may have missed - perhaps if you post where you have got, thus far? Also - have a read of this codeproject article on the WPF treeview control... it has helped me no end :) http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx – kiwipom Dec 09 '09 at 08:19