zoukankan      html  css  js  c++  java
  • 使用代码浏览WPF控件模版

    纯代码创建,不需要创建界面,创建WPF工程后,直接复制代码就可以使用。

    当你手头没有Blend,又不记得以下这段代码,但是又想浏览控件模版的时候,就可以直接复制拿来用了。

     1   public partial class MainWindow : Window
     2     {
     3         ListBox lbox;
     4         TextBox tbox;
     5         Grid grid;
     6         public MainWindow()
     7         {
     8             InitializeComponent();
     9             InitializeControl();
    10             LoadControlTemplate();
    11 
    12         }
    13 
    14         /// <summary>
    15         /// 创建界面
    16         /// </summary>
    17         private void InitializeControl()
    18         {
    19             grid = new Grid();
    20             ColumnDefinition col1 = new ColumnDefinition();
    21             ColumnDefinition col2 = new ColumnDefinition();
    22             col1.Width = GridLength.Auto;
    23             grid.ColumnDefinitions.Add(col1);
    24             grid.ColumnDefinitions.Add(col2);
    25 
    26             //List
    27             lbox = new ListBox();
    28             lbox.SelectionChanged += (a, b) => { ShowControlTemplate(); };
    29             //TextBox
    30             tbox = new TextBox();
    31             tbox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
    32             tbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Auto;
    33 
    34             grid.Children.Add(lbox);
    35             grid.Children.Add(tbox);
    36 
    37             Grid.SetColumn(lbox, 0);
    38             Grid.SetColumn(tbox, 1);
    39 
    40             this.Content = grid;
    41         }
    42 
    43         private void LoadControlTemplate()
    44         {
    45             Type type = typeof(System.Windows.Controls.Control);
    46             List<Type> controlType = new List<Type>();
    47             System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(System.Windows.Controls.Control));
    48 
    49             foreach (Type item in assembly.GetTypes())
    50             {
    51                 if (item.IsSubclassOf(type) && !item.IsAbstract && item.IsPublic)
    52                 {
    53                     controlType.Add(item);
    54                 }
    55             }
    56             lbox.ItemsSource = controlType;
    57         }
    58 
    59         private void ShowControlTemplate()
    60         {
    61             try
    62             {
    63                 Type type = (Type)lbox.SelectedItem;
    64                 System.Reflection.ConstructorInfo info = type.GetConstructor(Type.EmptyTypes);
    65                 Control control = (Control)info.Invoke(null);
    66                 control.Visibility = Visibility.Collapsed;
    67                 grid.Children.Add(control);
    68                 ControlTemplate template = control.Template;
    69 
    70                 System.Xml.XmlWriterSettings setting = new System.Xml.XmlWriterSettings();
    71                 setting.Indent = true;
    72                 StringBuilder sb = new StringBuilder();
    73                 System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(sb, setting);
    74                 System.Windows.Markup.XamlWriter.Save(template, writer);
    75                 tbox.Text = sb.ToString();
    76                 grid.Children.Remove(control);
    77 
    78             }
    79             catch (Exception ex)
    80             {
    81                 tbox.Text = ex.Message;
    82             }
    83         }
    84     }

     截图:

  • 相关阅读:
    菜根谭#69
    菜根谭#68
    菜根谭#67
    菜根谭#66
    菜根谭#65
    菜根谭#64
    菜根谭#63
    更新centos本地仓库(换源)
    docker初探
    centos python版本升级到3.x
  • 原文地址:https://www.cnblogs.com/zhaotianff/p/8108151.html
Copyright © 2011-2022 走看看