• WPF标准控件模板查看程序(文件里面)


    xaml

    <Window x:Class="ControlTemplateBrowser.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="500" Width="1000">
        <Grid Name="grid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="7*"/>
            </Grid.ColumnDefinitions>
            
            <ListBox x:Name="lstTypes" DisplayMemberPath="Name" SelectionChanged="lstTypes_SelectionChanged"/>
            <TextBox x:Name="txtTemplate" Grid.Column="1" TextWrapping="Wrap" VerticalScrollBarVisibility="Visible" FontFamily="Consolas"/>
        </Grid>
    </Window>
    View Code

    cs

    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.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Reflection;
    using System.Xml;
    using System.Windows.Markup;
    
    namespace ControlTemplateBrowser
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            }
    
            void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                Type controlType=typeof(Control);
    
                List<Type> derivedTypes = new List<Type>();
    
                //Search all the types in the assembly where the control class is defined
                Assembly assembly = Assembly.GetAssembly(typeof(Control));
    
                foreach (Type type in assembly.GetTypes())
                {
                    //only add a type of the list if it's a control, a concrete class,
                    //and public
                    if (type.IsSubclassOf(controlType)&&!type.IsAbstract&&type.IsPublic)
                    {
                        derivedTypes.Add(type);
                    }
                }
    
                //sort the types. the custom typeComparer class orders types
                //alphabetically by type name
    
                derivedTypes.Sort(new TypeComparer());
    
                lstTypes.ItemsSource = derivedTypes;
    
            }
    
            private void lstTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
    
                try
                {
                    //get the selected 
                    Type type = (Type)lstTypes.SelectedItem;
    
                    //Instantiate the type
                    ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
    
                    Control control = (Control)info.Invoke(null);
    
                    //Window win = control as Window;
                    //if (win != null)
                    //{
                    //    // Create the window (but keep it minimized).  
                    //    win.WindowState = System.Windows.WindowState.Minimized;
                    //    win.ShowInTaskbar = false;
                    //    win.Show();
                    //}
                    //else
                    //{
                        // Add it to the grid (but keep it hidden).  
                        control.Visibility = Visibility.Collapsed;
                        grid.Children.Add(control);
                    //}
    
                    // Get the template.  
                    ControlTemplate template = control.Template;
    
                    // Get the XAML for the template.  
                    XmlWriterSettings settings = new XmlWriterSettings();
                    settings.Indent = true;
                    StringBuilder sb = new StringBuilder();
                    XmlWriter writer = XmlWriter.Create(sb, settings);
                    XamlWriter.Save(template, writer);
    
                    // Display the template.  
                    txtTemplate.Text = sb.ToString();
    
                    // Remove the control from the grid.  
                    //if (win != null)
                    //{
                    //    win.Close();
                    //}
                    //else
                    //{
                        grid.Children.Remove(control);
                    //}  
    
    
                }
                catch (Exception err)
                {
    
                    txtTemplate.Text = "<<>Error generating template:" + err.Message+ ">";
                }
    
            }
        }
    }
    View Code

    TypeComparer

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace ControlTemplateBrowser
    {
        public class TypeComparer : IComparer<Type>
        {
            public int Compare(Type x, Type y)
            {
                if (x == null || y == null)
                    throw new ArgumentException("参数不能为空");
                if (x.Name.CompareTo(y.Name) != 0)
                {
                    return x.Name.CompareTo(y.Name);
                }           
                else
                {
                    return 0;
                }
            }
        }
    }
    View Code
  • 相关阅读:
    使用滤镜设置透明导致 IE 6/7/8/9 解析异常
    用事实证明cssText性能高
    字符串转成数字的多种方式
    IE6/7/8/9怪异模式和IE6标准模式中多余字符Bug
    将HTMLCollection/NodeList/伪数组转换成数组
    servlet下利用jsonlib
    JavaScript中同名标识符优先级
    JavaScript继承方式(3)
    设置HTML元素的透明度
    各浏览器中定位元素遮盖其它元素差异性
  • 原文地址:https://www.cnblogs.com/FaDeKongJian/p/3175958.html
Copyright © 2020-2023  润新知