2

In my View I have a TreeView with a event "TreeView_MouseLeftButtonDown". If it fires it proofs if the mouse clicked on a TreeViewItem. If not it deselects the last TreeViewItem. My question is, should i do this in the code-behind or call a static methode in the viewmodel-class? How would you solve this?

The Methode:

private void treeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    if (sender != null)
    {
        var treeView = sender as TreeView;
        if (treeView != null && treeView.SelectedItem != null)
            TreeViewHelper.ReturnTreeViewItem(ref treeView, (XmlNode)treeView.SelectedItem).IsSelected = false;
    }
} 

XAML:

<TreeView ... KeyDown="TreeView_KeyDown" 
              MouseLeftButtonDown="TreeView_MouseLeftButtonDown" 
              SelectedItemChanged="TreeView_SelectedItemChanged" />
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
jwillmer
  • 3,570
  • 5
  • 37
  • 73

2 Answers2

2

You are trying to add a behaviour to the TreeView.

The way I would implement this would be using Attached Properties. I would create an attached property called VerifiesLeftClick or similar and implement the logic in there. This way you do not need an event in the code behind.

See here for samples.

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 2
    **here** doesn't link to anything! I'm interested in the samples too :) – Ian Apr 11 '11 at 10:04
  • 1
    i have read the article and googelt for more but i don´t get through it :-( would be nice if you can show me the implementation on my problem :-) – jwillmer Apr 11 '11 at 13:55
  • @myName I was about to post my solution but code from Pawel looks correct. – Aliostad Apr 11 '11 at 17:41
0

I made for you solution using attached behaviors which were pretty well described here Introduction to Attached Behaviors in WPF by Josh Smith

My solution:

public static class TreeViewBehavior
{
    public static bool GetIsResetMouseLeftButtonDown(TreeView treeView)
    {
        return (bool)treeView.GetValue(IsResetMouseLeftButtonDownProperty);
    }
    public static void SetIsResetMouseLeftButtonDown(TreeView treeViewItem, bool value)
    {
        treeViewItem.SetValue(IsResetMouseLeftButtonDownProperty, value);
    }
    public static readonly DependencyProperty IsResetMouseLeftButtonDownProperty =
        DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDown", typeof(bool), typeof(TreeViewBehavior),
        new UIPropertyMetadata(false, OnIsMouseLeftButtonDownChanged));
    static void OnIsMouseLeftButtonDownChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TreeView item = depObj as TreeView;
        if (item == null)
            return;
        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
        {
            item.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
        }
        else
        {
            item.MouseLeftButtonDown -= OnMouseLeftButtonDown;
        }
    }
    static void OnMouseLeftButtonDown(object sender, RoutedEventArgs e)
    {
        var tempItem = e.Source as TreeViewItem;
        if (tempItem != null && tempItem.IsSelected == false)
        {
            tempItem.IsSelected = true;
        }
        else
        {
            var tree = e.Source as TreeView;
            if (tree != null && tree.SelectedItem != null)
            {
                var selItem = (tree.SelectedItem as TreeViewItem);
                if (selItem != null)
                {
                    selItem.IsSelected = false;
                }
            }
        }
    }
}

and then in View you should add this:

<TreeView local:TreeViewBehavior.IsResetMouseLeftButtonDown="True">

I hope my solution do what you are trying to achieve.

PawelSt
  • 826
  • 1
  • 10
  • 6
  • 1
    Wow, that is mega-complicated for such a simple thing. – Damien Apr 12 '11 at 05:20
  • I agree that it could be done in code-behind, but some people like having clean code-behind. My solution also gives opportunity to easy reuse behaviour in other treevies. – PawelSt Apr 12 '11 at 05:29
  • your deselect-function doesent work but the behavior works great. thx for your cost. – jwillmer Apr 12 '11 at 07:34
  • @myName what do you mean by telling that my deselect-function doesn't work? Are you sure that you are using my latest code? – PawelSt Apr 12 '11 at 07:53
  • I have bindet a Xml to my TreeView this is way i need a custom function for deselect ;-) Solved here: [link](http://stackoverflow.com/questions/5407650/how-to-select-a-databound-treeviewitem) – jwillmer Apr 12 '11 at 08:26