一般在哪里可以找到 WPF DataGrid 或 WPF 控件的 XAML 模板? [英] Where can I find the XAML template for a WPF DataGrid or a WPF control in general?

查看:22
本文介绍了一般在哪里可以找到 WPF DataGrid 或 WPF 控件的 XAML 模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改 DataGrid(4.0 版)的控件模板,但我不确定现有模板的外观.有谁知道它是什么或我在哪里可以找到它?

I would like to modify the control template for the DataGrid (4.0 version) but I am not sure what the existing template looks like. Does anyone know what it is or where I could find it?

一般来说:对于某些 WPF 控件,如何查看相应控件模板的默认内容?

In general: For some WPF control, how can I see the default content the respective control template?

推荐答案

我建议进入 Blend(如果有)并提取模板.否则,您可以使用 XAMLWriter 获取 XAML.

I would recommend going into Blend (if you have it) and extracting the Template. Otherwise, you can get the XAML using XAMLWriter.

这个不幸命名的页面(我不认为这真的是孩子们的东西",是吗?)有一个同时做这两件事的例子.

This unfortunately named page (I don't think this is really "Kid's Stuff", do you?) has an example of doing both of these things.

万一页面碰巧消失了(后视镜中的 2009 年已经很远了):

In case the page does happen to go away (2009 is pretty far in the rear-view mirror):

// Get the template from the control
YourControlType ctl = new YourControlType();
ctl.UpdateDefaultStyle();
ControlTemplate template = ctl.Template;

// We want our xaml of be properly indented, ohterwise
// we would not be able to indent them.
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;

// Make the string builder
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, xmlSettings);
XamlWriter.Save(template, writer);


// Now the sb.ToString() should give us the template

所有功劳都归功于 Shafqat Ahmed.

All credit to Shafqat Ahmed.

这篇关于一般在哪里可以找到 WPF DataGrid 或 WPF 控件的 XAML 模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆