• 使用代码浏览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     }

     截图:

  • 相关阅读:
    PHP中的trait
    Laravel中的队列
    微信小程序开发常见问题
    python学习day1
    javaScript实现栈的创建,添加元素,检查栈是否为空,删除元素,查看栈顶元素,清空栈元素,toString方法,通过栈实现多种进制转换
    join&concat&splice&slice&indexOf&lastindexOf&charCodeAt&fromCharCode
    使用函数自动创建表格
    两种方式实现随机颜色
    多选全选框使用addEvenListener实现
    使用setInterval来控制正方形的移动的频率(每隔1秒),鼠标点击停止移动,当再次点击恢复移动
  • 原文地址:https://www.cnblogs.com/zhaotianff/p/8108151.html
Copyright © 2020-2023  润新知