文档章节

wpf - utilities to get the default Control temp...

j
 joe.bq.wang
发布于 2013/05/14 11:55
字数 538
阅读 40
收藏 0
wpf

it is a common scenario when you sometime want to change the default template for a built-in controls, for example, when you are styling a built-in control or when you are extending a default control to fullfil some new features (adding new capability).
However, it is not exposed on the default template definition, and because of the xaml definition is closed to the client, you won't be able to reflect and view the content of the Xaml to get what you want.. 
but the world is not closed to you, Control.Template property exposed the compiled Control template, the only thing you need to do is to feed that to a XamlWriter. and this is viable. 
In this post, I will reference the post from the Book "Pro Wpf in C# 2010 - Windows Presentation Foundation in .Net 4.0" , in chapter 17, Control template.  
Basically the left is a explorer, which list all the controls, and the right is the Viewer Box, which has the template rendered as String.

Here is the code:

<Window x:Class="ControlTemplateExplorer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="2"/>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

            <ListView x:Name="listTypes" Grid.Column="0" Grid.Row="0" DisplayMemberPath="Name" SelectionChanged="listTypes_SelectionChanged"/>
        
        <GridSplitter 
            ResizeDirection="Columns"
            VerticalAlignment="Stretch"
            HorizontalAlignment="Stretch"
            ShowsPreview="True"
            Grid.Column="1"
            />
        <!-- ViewBox ??-->
        <!-- DocumentViewer ??-->
        <!--http://stackoverflow.com/questions/1981137/c-sharp-wpf-scrollviewer-textblock-troubles-->
        <ScrollViewer  Grid.Column="2" Grid.Row="0"
                       HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" 
                       x:Name="Scroller"
                       >
            <TextBox x:Name="txtTemplate" 
                     HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                     MinWidth="100" Width="{Binding ElementName=Scroller, Path=ViewportWidth}" TextWrapping="Wrap"
                     />
        </ScrollViewer>

        <Grid x:Name="grid" Visibility="Collapsed" >
        </Grid>
    </Grid>
</Window>

and in the code behind, we have this; 

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml;

namespace ControlTemplateExplorer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Loaded += Window_Loaded;
        }

        private void Window_Loaded(object sender, EventArgs e)
        {
            Type controlType = typeof (Control);

            List<Type> derivedTypes = new List<Type>();

            Assembly assembly = Assembly.GetAssembly(typeof (Control));

            // Search all the types in teh assembly where the Control class is defined. 
            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());

            // How the list of the types
            listTypes.ItemsSource = derivedTypes;
        }

        private void listTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                // Get the Selected type.
                Type type = (Type) listTypes.SelectedItem;

                // Instance the type.
                ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
                Control control = (Control)info.Invoke(null);

                // 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
                grid.Children.Remove(control);
            }
            catch (Exception err)
            {
                txtTemplate.Text = "<Error generating template: " + err.Message + " >";
            }
        }
    }

    class TypeComparer : IComparer<Type>
    {
        #region Implementation of IComparer<in Type>

        public int Compare(Type x, Type y)
        {
            return string.Compare(x.Name, y.Name);
        }

        #endregion
    }
}
When the program runs, it has the following. 



© 著作权归作者所有

j
粉丝 10
博文 183
码字总数 80491
作品 0
浦东
私信 提问
WebBrowser脚本错误的原因及Web Browser Control & Specifying the IE Version

最近想用 WebBrowser控件写个程序访问中国南方电网,以便查询各个网点停电计划停电信息,但发现报脚本错误,导致无法正常访问,而IE访问却无此问题。一开始抱着头疼医头,脚疼医脚的心态,想...

Alien_Dog
2018/01/14
25
1
Atcoder Tenka1 Programmer Beginner Contest IntegerotS 【异或+思维】

IntegerotS Time Limit: 4000/2000 MS (Java/Others) Memory Limit:262144/262144 K (Java/Others) Problem Description Seisu-ya, a store specializing in non-negative integers, sellsN ......

my_sunshine26
2017/10/01
0
0
【干货来了】2016年DevExpress资源汇总(示例、视频)

在2016年,著名软件界面解决方案专家DevExpress进行了重大升级,EVGET围绕版本升级推出了一系列干货资源,现将2016年所有Dev示例资源和视频资源汇总于此,欢迎收藏转发点赞哦~ 示例Demo(仍在...

zoujiajun33
2016/12/02
83
0
DevExpress v15.1:WPF控件升级(三)

<下载最新版DevExpress WPF Controls v15.1.5> WPF Rich Editor Control 编辑注释 最终用户可以添加新的注释、编辑它们,如果用户不再需要可以隐藏或删除注释。 布局API 在这个版本中,我们引...

Miss_Hello_World
2015/08/24
299
0
WPF Ribbon 开发资料分享

Ribbon 开发工具 WPF Ribbon New 目前微软发布的最新Ribbon 开发工具,上一篇已经做过介绍这里就不再多说了。支持WPF 3.5 SP1、WPF 4,全面兼容VS2010 和Blend4。 下载 Fluent Ribbon Contro...

junwong
2012/03/09
329
0

没有更多内容

加载失败,请刷新页面

加载更多

Mybatis Plus删除

/** @author beth @data 2019-10-17 00:30 */ @RunWith(SpringRunner.class) @SpringBootTest public class DeleteTest { @Autowired private UserInfoMapper userInfoMapper; /** 根据id删除......

一个yuanbeth
今天
4
0
总结

一、设计模式 简单工厂:一个简单而且比较杂的工厂,可以创建任何对象给你 复杂工厂:先创建一种基础类型的工厂接口,然后各自集成实现这个接口,但是每个工厂都是这个基础类的扩展分类,spr...

BobwithB
今天
4
0
java内存模型

前言 Java作为一种面向对象的,跨平台语言,其对象、内存等一直是比较难的知识点。而且很多概念的名称看起来又那么相似,很多人会傻傻分不清楚。比如本文我们要讨论的JVM内存结构、Java内存模...

ls_cherish
今天
4
0
友元函数强制转换

友元函数强制转换 p522

天王盖地虎626
昨天
5
0
js中实现页面跳转(返回前一页、后一页)

本文转载于:专业的前端网站➸js中实现页面跳转(返回前一页、后一页) 一:JS 重载页面,本地刷新,返回上一页 复制代码代码如下: <a href="javascript:history.go(-1)">返回上一页</a> <a h...

前端老手
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部