Click here to Skip to main content
15,897,090 members
Articles / Web Development / HTML

My Personal Commander Variant

Rate me:
Please Sign up or sign in to vote.
4.66/5 (13 votes)
5 Oct 2016CPOL14 min read 44.7K   1.2K   47  
A C#/WPF application for displaying folders on a grid and performing combined functions on them.
// This file is part of the Lobster.MVVM library
// Copyright: Andreas Raczek
// This file is published under the The Code Project Open License (CPOL) 
// See the file "CPOL.html" for the full license governing this code.

using System.Windows;
using System.Windows.Controls;

namespace Lobster.MVVM
{
    /// <summary> 
    /// This is not working as the bound element must be changed to raise the 
    /// SelectedItemChanged callback. If its bound to a null object waiting to
    /// be changed by the behavior itself, nothing will happen.
    /// </summary>
    public class TreeViewBehavior
    {
#region SelectedItem Property
        public static object GetSelectedItem(DependencyObject obj)
        {
            return (object) obj.GetValue(SelectedItemProperty);
        }

        public static void SetSelectedItem(DependencyObject obj, object value)
        {
            obj.SetValue(SelectedItemProperty, value);
        }

        public static readonly DependencyProperty SelectedItemProperty 
            = DependencyProperty.RegisterAttached(
                "SelectedItem", 
                typeof(object), 
                typeof(TreeViewBehavior), 
                new FrameworkPropertyMetadata(new PropertyChangedCallback(SelectedItemChanged)
                    , FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
                    ));
                  
        private static void SelectedItemChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var treeview = sender as TreeView;

            if (treeview != null)
            {
                if (e.NewValue != null) 
                {
                    // Attach Item Change Handler
                    treeview.SelectedItemChanged += OnTreeViewSelectedItemChanged;
                } else {
                    // Detach Item Change Handler
                    treeview.SelectedItemChanged -= OnTreeViewSelectedItemChanged;
                }
            }
        }

        private static void OnTreeViewSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            /*
            SetSelectedItem2(sender as TreeView, e.NewValue);
            
            var dob = sender as DependencyObject;
            dob.SetCurrentValue(SelectedItemProperty, e.NewValue);
             */
        }
#endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions