Click here to Skip to main content
15,896,453 members
Articles / Desktop Programming / WPF

Visual Studio 2012 Metro Styles for WPF

Rate me:
Please Sign up or sign in to vote.
4.77/5 (75 votes)
9 Dec 2012CPOL8 min read 363.4K   60.8K   222  
Black Metro Styles for Button, ListBox, Menu, ScrollBar, TabControl, TextBox, ComboBox, DataGrid and GroupBox
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Collections.ObjectModel;

namespace Selen.Wpf.GenericAppTree
{
    public class AppTreeContainer : AppTreeNode
    {
        private readonly ObservableCollection<AppTreeNode> _children;
        private readonly bool _enablePreview;

        private AppTreeNode _previewChild;

        public AppTreeContainer(string selectionGroup, string header, bool enablePreview = false)
            : base(selectionGroup, header)
        {
            _children = new ObservableCollection<AppTreeNode>();
            _enablePreview = enablePreview;

            if(_enablePreview)
            {
                PropertyChanged += (sender, args) =>
                {
                    if (args.PropertyName != "IsSelected") return;
                    
                    if(!IsSelected)
                    {
                        foreach (var item in Children)
                        {
                            item.IsSelected = false;
                        }
                    }

                    UpdatePreview();
                };
            }
        }

        public IEnumerable<AppTreeContainer> ContainerChildren
        {
            get
            {
                return _children.OfType<AppTreeContainer>();
            }
        }

        public IEnumerable<AppTreeItem> ItemChildren
        {
            get
            {
                return _children.OfType<AppTreeItem>();
            }
        }

        public IEnumerable<AppTreeNode> Children
        {
            get
            {
                return _children;
            }
        }

        public void AddChild(AppTreeNode item)
        {
            item.SelectionChanged += OnSelectionChanged;
            item.Parent = this;
            _children.Add(item);

            if (_enablePreview) UpdatePreview();
        }

        public bool RemoveChild(AppTreeNode item)
        {
            item.SelectionChanged -= OnSelectionChanged; 
            item.Parent = null;
            var result = _children.Remove(item);

            if (_enablePreview) UpdatePreview();
            return result;
        }

        private void UpdatePreview()
        {
            if (_previewChild != null) _previewChild.PropertyChanged -= PreviewPropertyChanged;
            
            if(_children.Count == 0)
            {
                Icon = null;
                OnPropertyChanged("Icon");

                return;
            }

            _previewChild = _children.First();
            _previewChild.PropertyChanged += PreviewPropertyChanged;

            if (IsSelected)
            {
                Icon = null;
                OnPropertyChanged("Icon");
            }
            else
            {
                Icon = _previewChild.Icon;
                OnPropertyChanged("Icon");
            }
        }

        private void PreviewPropertyChanged(object sender, PropertyChangedEventArgs args)
        {
            if (args.PropertyName == "Icon") UpdatePreview();
        }
    }
}

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
Student
Germany Germany
I´m a student and hobby programmer from Germany.

Comments and Discussions