wpf 如何在 Visual Studio 中提取默认控件模板?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8825030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Extract Default Control Template In Visual Studio?
提问by foreyez
I'm wondering how I can extract (get a copy) of the Default Template of a given control using Visual Studio. I know this can be done with Expression Blend (right click a control, "Edit Template" -> "Edit a Copy...") which then copies the default control template in my Xaml. But can this be done with Visual Studio at all?
我想知道如何使用 Visual Studio 提取(获取副本)给定控件的默认模板。我知道这可以通过 Expression Blend 完成(右键单击一个控件,“编辑模板”->“编辑副本...”),然后将默认控件模板复制到我的 Xaml 中。但是这完全可以用 Visual Studio 来完成吗?
回答by Sheridan
2015 update with clear instructions
2015 更新有明确说明
In Visual Studio 2013, you can get the default ControlTemplate
of a control in a few simple steps.
在 Visual Studio 2013 中,您可以通过ControlTemplate
几个简单的步骤获得控件的默认值。
In the WPF designer, select the relevant control, or place the mouse cursor on the relevant control in the XAML.
Press F4to open the Properties Window.
Open the Miscellaneouscategory to find the Templateproperty, or type Templatein the search field at the top of the Window.
Click on the little square to the right of the Templatefield and select the Convert to New Resource...option:
在 WPF 设计器中,选择相关控件,或将鼠标光标放在 XAML 中的相关控件上。
按F4打开“属性”窗口。
打开杂项类别以查找模板属性,或在窗口顶部的搜索字段中键入模板。
单击Template字段右侧的小方块,然后选择Convert to New Resource...选项:
- In the popup dialog, name the new
ControlTemplate
to be added and decide where you want it to be defined:
- 在弹出对话框中,
ControlTemplate
为要添加的新命名并决定要在何处定义它:
- Click on the OKbutton.
- 单击确定按钮。
回答by Royi Mindel
From Visual studio - right click the control, choose properties,
从 Visual Studio - 右键单击控件,选择属性,
In the properties window look for the Template Property and right click it, choose Extract Value To Resource
在属性窗口中查找模板属性并右键单击它,选择将值提取到资源
That will create a copy of the template in the XAML for you to work on.
这将在 XAML 中创建模板的副本供您处理。
回答by erodewald
Just to update this question, in VS 11 the XAML designer allows you to do this just like Expression Blend.
只是为了更新这个问题,在 VS 11 中,XAML 设计器允许您像 Expression Blend 一样执行此操作。
回答by Adi Lester
As far as I know it's not possible. However, you can use Show Me The Templateto view the default template for a given control.
据我所知这是不可能的。但是,您可以使用Show Me The Template查看给定控件的默认模板。
回答by John Christopher Linstrum
One thing to keep in mind: if you already have a style defined somewhere that targets the given control then all of the above described options will be disabled. I had the following bit of code in my App.xaml file:
要记住的一件事:如果您已经在某处定义了一个针对给定控件的样式,那么上述所有选项都将被禁用。我的 App.xaml 文件中有以下代码:
<Application.Resources>
<Style TargetType="Button">
<Setter Property="IsTabStop" Value="False"/>
</Style>
</Application.Resources>
I was pulling my hair out trying to figure out why the edit a copy...
and convert to new resource...
options described in the other answers were disabled for my Button (which was defined in a completely different file). I commented out the above style code for Button and suddenly both options weren't disabled anymore.
我正在努力弄清楚为什么我的 Button(在一个完全不同的文件中定义)禁用了其他答案中描述的edit a copy...
和convert to new resource...
选项。我注释掉了 Button 的上述样式代码,突然这两个选项都不再禁用了。
Moral of the story: VS won't generate a style containing a template copy for you if a style already exists for that element anywhere in your code.
故事寓意:如果代码中任何位置的元素已经存在样式,VS 将不会为您生成包含模板副本的样式。