Click here to Skip to main content
15,881,709 members
Articles / Desktop Programming / WPF

Style Browser for WPF

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
25 Apr 2011CPOL5 min read 37K   2.6K   31  
Application for previewing XAML styles in WPF.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.IO;
using StyleBrowser.Controls;


namespace StyleBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<DictionaryEntry> entries = new List<DictionaryEntry>();

        public object CreateObj(Type t)
        {
            return Activator.CreateInstance(t);
        }

        public MainWindow()
        {
            InitializeComponent();  
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();

            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ClearPanels();
                entries.Clear();

                try
                {

                    XamlReader reader = new XamlReader();
                    FileStream stream = new FileStream(dlg.FileName, FileMode.Open);
                    Uri uri = new Uri((new FileInfo(dlg.FileName)).Directory.FullName + "\\");
                    object xaml = reader.LoadAsync(stream, new ParserContext() { BaseUri = uri });


                    stream.Close();
                    stream.Dispose();
                    this.Resources.Clear();
                    if (xaml is ResourceDictionary)
                    {
                        foreach (DictionaryEntry item in (xaml as ResourceDictionary))
                        {
                            this.Resources.Add(item.Key, item.Value);
                            entries.Add(item);                           
                        }
                    }
                    ReadXaml(entries);
                }
                catch
                {
                   MessageBox.Show("Cant parse XAML. It has non-standrad namespaces or references to another resources");
                }
            }
        }

        private void ReadXaml(List<DictionaryEntry> list)
        {
            foreach (var item in list.OrderBy(e => e.Key.ToString()))
            {
                StyleItem styleItem = null;
                DictionaryEntry entry = (DictionaryEntry)item;
                if (entry.Value is Style)
                {
                    Style style = entry.Value as Style;
                    Type type = style.TargetType;
                    object obj = CreateObj(type);

                    if (obj is AnimationTimeline) continue;

                    styleItem = new StyleItem(obj as FrameworkElement, style, type, entry.Key.ToString());

                    if (type == typeof(Button) || type == typeof(ToggleButton))
                    {
                        buttonsPanel.Children.Add(styleItem);
                    }
                    else if (type == typeof(Label))
                    {
                        textPanel.Children.Add(styleItem);
                    }
                    else if (type == typeof(TextBlock))
                    {
                        textPanel.Children.Add(styleItem);
                    }
                    else if (type == typeof(Image))
                    {
                        imagesPanel.Children.Add(styleItem);
                    }
                    else if (type == typeof(Window))
                    {
                        windowsPanel.Children.Add(styleItem);
                    }
                    else otherPanel.Children.Add(styleItem);

                }

                if (entry.Value is Brush)
                {
                    styleItem = new StyleItem(entry.Value as Brush, entry.Key.ToString());
                    brushesPanel.Children.Add(styleItem);
                    styleItem.ToolTip = XamlWriter.Save(entry.Value).Replace(">", ">\n");
                }

                if (styleItem != null)
                {
                    string xaml;
                    try
                    {
                        xaml = XamlWriter.Save(entry.Value).Replace(">", ">\n").NormalizeXaml();
                    }
                    catch { xaml = "XAML cannot be parsed"; }
                    styleItem.Xaml = xaml;
                }
            }
        }

        private void ClearPanels()
        {
            buttonsPanel.Children.Clear();
            textPanel.Children.Clear();
            imagesPanel.Children.Clear();
            brushesPanel.Children.Clear();
            contentsPanel.Children.Clear();
            otherPanel.Children.Clear();
            windowsPanel.Children.Clear();
        }

        private void Black_Click(object sender, RoutedEventArgs e)
        {
            tcStyles.Background = Brushes.Black;
        }

        private void Grey_Click(object sender, RoutedEventArgs e)
        {
            tcStyles.Background = new SolidColorBrush(Color.FromRgb(180,180,180));
        }

        private void White_Click(object sender, RoutedEventArgs e)
        {
            tcStyles.Background = Brushes.White;
        }

        private void tbSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            ClearPanels();
            if (tbSearch.Text == String.Empty)
            {
                ReadXaml(entries);
            }
            else
            {
                ReadXaml(entries.Where(en=>en.Key.ToString().ToLower().Contains(tbSearch.Text.ToLower())).ToList());
            }
        }
    }
}

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 (Senior) Luxoft
Russian Federation Russian Federation
My developers career starts in 1995, when my father bought my first PC and gave my book "QBasic".
Than it was Pascal, Delphi, C++ Builder...But when I began to work with .Net in 2008, I understand, that this technology is exactly what I need Smile | :)

For now, I am a Senior .Net developer at Luxoft.

P.S. Right now I am extremly looking for a legal job offer from USA.

Comments and Discussions