How to Extract Default Control Template In Visual Studio? - wpf

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?

2015 update with clear instructions
In Visual Studio 2013, you can get the default ControlTemplate of a control in a few simple steps.
In the WPF designer, select the relevant control, or place the mouse cursor on the relevant control in the XAML.
Press F4 to open the Properties Window.
Open the Miscellaneous category to find the Template property, or type Template in the search field at the top of the Window.
Click on the little square to the right of the Template field and select the Convert to New Resource... option:
In the popup dialog, name the new ControlTemplate to be added and decide where you want it to be defined:
Click on the OK button.
EDIT >>>
In Visual Studio 2019 and later, this option seems to be disabled for some reason. A workaround can be found by right-clicking the control in the design view and selecting "Edit Template", then selecting "Edit a Copy...".

From Visual studio - right click the control, choose properties,
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.

Just to update this question, in VS 11 the XAML designer allows you to do this just like Expression Blend.

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:
<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.
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.

In VS19 I wasn't able to do this through properties.
However, I was able to right click the control in design mode Edit Template and Edit a Copy.

As far as I know it's not possible. However, you can use Show Me The Template to view the default template for a given control.

Related

Easily Editing XAML Color Resources in Visual Studio

From the XAML designer in Visual Studio it is possible to place your cursor into the Color attribute of a Brush element and easily access the color Editor.
<SolidColorBrush x:Key="MyBrush" Color="#FF4B4B4B" />
In the above case, if you place the cursor before in the # in the XAML, then you can access the color from the properties view shown in the image below, it is easily accessed under the "Appearance" section.
That works great, the issue is only when one would like to do the same with a Color resource that is defined like this instead.
<Color x:Key="MyBrushColor">#FF4D7BBF</Color>
In the above case, placing the cursor in front of the # apparently does NOT cause Visual Studio to display the color editor anywhere in the properties view.
Right after I posted this, I see how to do it, hope this helps someone else.
Place the cursor in the Color element.
Right-click on the "Color" box in the Properties View and select Edit Resource...

Visual studio 2015: How to have by default the XAML code, not the split view?

I don't use MUCH the XAML designer, but I appreciate at some time to have the possibility to use, and more, to have the "Properties" windows present to set some properties.
But I would like that when I click on a XAML file, I get the XAML code, without the GUI part. Basically, the Xaml Designer, but not in "split" view.
Is this possible?
I found that it is possible to totally disable the XAML designer, but then:
I don't get the possibility to just switch to this view(well with Open Width, yes)
More important, I don't have the "Properties" windows populated with the properties of the currently selected XAML
I also found that there is a setting with a default view(Tools, Options, XAML Designer, Default Document view: SourceView), but this doesn't work(or I didn't understood), I still get the default split view, even after a VS2015 restart.
So is it possible to keep the XAML designer, but have by default only the XAML displayed?
Easy Way.
Go to Tools -> Options.
and follow the screen shot.
To open .xaml files in XAML-only mode, you need to change the default editor that .xaml files open with. Here's how:
Right click on any .xaml file.
Click on Open with...
Select Source Code (Text) Editor.
Click Set as Default.
Click OK.
That's it. Opening any .xaml file should now open in full XAML view. To get back to the designer, simply press Shift + F7, or Right Click -> View Designer.
The only drawback that I have experienced is that Intellisense doesn't seem to pick up any resources (i.e DynamicResource and StaticResource references) that you may have.
If you still want the Designer running in the background, use the following instructions:
Go to Tools -> Options.
Expand Text Editor -> XAML -> Miscellaneous.
Check the Always open documents in full XAML view checkbox.
This will open all .xaml documents in full XAML view, to switch back to the designer, simply click on the Design tab.
New place XAML Designer options.
screenshot

How to style RibbonComboBox

I have described my problems with binding the SelectedItem of the RibbonComboBox. Another very ugly issue arises when trying to style this control.
Starting with the unstyled ComboBox from the post mentioned above:
<r:RibbonComboBox >
<r:RibbonGallery SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<r:RibbonGalleryCategory ItemsSource="{Binding Controls}" DisplayMemberPath="Caption" />
</r:RibbonGallery>
</r:RibbonComboBox>
I get the expected result:
But when applying a style with the help of Microsoft Blend (Preview for VS 2012), Object | Edit Style | Edit a Copy... (copying only the Template, with Blend or Visual Studio, has the same effect):
The selected item will no longer be displayed correctly - instead of the DisplayMemberPath property, the type name is shown. Items in the dropdown are still correct. Note that I did not yet change the style in any way, it is the default style/template that gets extracted that will produce this issue.
<r:RibbonComboBox Style="{DynamicResource RibbonComboBoxStyle1}" >
I also had trouble to style my dropdown items (mainly wanted to change the mouseover background cornerradius), because the ItemContainerStyle for the RibbonComboBox will have target type RibbonMenuItem and does not seem to have an effect on the actual RibbonGalleryItems.
How to fix the RibbonComboBox style?
How can I get to the RibbonGalleryItem style by way of Blend/VS?
Currently, I have solved this issue by extracting the default RibbonComboBox style directly from RibbonControlsLibrary.dll (Resources/RibbonControlsLibrary.g.resources/themes/generic.baml) with ILSpy.
This is tedious work, because the style references other StaticResources in the same file, so you need to find and extract those, too.
The same applies to the RibbonGalleryItem style, it can be extracted like this, but I would prefer a "cleaner" solution involving Blend or Visual Studio.
You may want to take a look at JetBrains dotPeek decompiler.
One of its features is:
"... decompile .baml files, enabling you to reconstruct XAML UI definitions."
In addition, I just finished to edit visual RibbonCombobox and its relative controls by using the decompiler mentioned above, which decompiled the entire Ribbon library including original Microsoft .XAML themes.

Adjusting width between image and label in RibbonMenuItem

I'm having some difficulty with styling the RibbonMenuButton in Microsoft Ribbon for WPF (October 2010).
Is there any way in which I can access the property for the width between the image and the label? I can't find any relevant member on the MSDN Property List, and as expected, adjusting the padding just changes the spacing between the entire button and it's container.
<ribbon:RibbonMenuButton SmallImageSource="/MyProject;component/Images/Foo.png" Label=Bar">
...
</ribbon:RibbonMenuButton>
Thanks in advance!
That will likely have to be done through the template. For template manipulation, I typically use Expression Blend as it allows easier access to the template and provides design time display of an example of the item being templated. If you don't have it, pick up a trial.
If you do get it, load your solution into Blend, right-click on an item you want to change the template for, selected Edit Template > Edit a Copy...
The template will be copied, referenced by the item, and you can then edit it to suit the look and feel you want for the control.

Is there a way to quickly create a style from a set of selected attributes?

I currently want to create a wpf style by selecting a group of attributes, e.g. Height, Width of a button, then right click and call a command like "Generate style from selection" and the IDE will do the rest for me: add .Resource tags, and a Style with sample x:Key and put the attributes' names and values into Setter tags.
Is it possible? Any addons are welcome. I prefer to have a free solution. Please discuss!
If you have Expression Blend at your disposal, creating a new style is fairly simple. Just a matter of stubbing out the style in code
<Style x:Name="MyStyle" TargetType="{x:Type TextBlock}"/>
Open the Resource browser, select the style you just made, and then use the properties box to set the properties.
As for Visual Studio, I have not yet seen a WPF extension that would do it even this automated. The XAML designer in Visual Studio leaves a lot to be desired for me, and one of the reasons is the style creation.
Blend has style visualization, VS2010 does not. Any styling stuff that I do, is in Blend.

Resources