Create a skin for controls in your project
You can customize the look of controls by using static resources to define templates that are applied to the controls. For example, you could create a template for a button that used images instead of rectangles to construct the appearance of the button.
To create a common look among different control templates or among different user controls, you can convert individual properties to static resources and then apply them to templates and user controls. For example, you could convert the border color of a button template to a resource and then apply that color resource to the border property of a check box template or a custom user control.
To reuse your templates and resources in other projects, you can move the resources into a resource dictionary file, and then add that file to other projects. The resource dictionary file becomes a repository for the skin of your application.
To create a template of a control
To customize the template of any control, you can use the procedures in Modify the appearance of a system control in different states and Modify the transition time between state changes.
The template becomes a resource that is listed in the Resources panel.
For information about how to modify a resource after you create it, see Modify a resource.
To create a user control
- To create a custom user control that you can design from scratch, you can use the procedures in Create a new user control in your project.
To convert a property to a static resource
Select an object on the artboard that has a property value that you want to reuse in other controls.
In the properties view of the Properties panel, locate the property that you want to reuse.
Suggerimento You can use the Search text box in the Properties panel to quickly locate a property by searching for characters in the name of the property.
The text that you type will filter the list of properties.
To restore the Properties panel, click Clear , next to the Search text box.
Do one of the following:
If you want to reuse a Brush, click Advanced options next to the brush property (such as the Fill or Background property), and then click Convert to New Resource.
If you want to reuse a color that is applied to a brush, select the brush if it is a Solid color brush , or select the gradient stop for the color you want to reuse if the brush is a Gradient brush . Then, click Convert color to resource .
If you want to reuse a numeric or other value type, click the Advanced options button next to the property, and then click Convert to New Resource.
In the dialog box that appears (titled Create <type> Resource), type a meaningful name for your resource, and then click OK.
The resource is created and listed in the Resources panel.
For information about how to modify a resource after you create it, see Modify a resource.
Apply a resource to another property
You have many ways to apply a resource to a property.
To apply a resource by dragging it from the Resources panel
From the Resources panel, drag a resource onto a control on the artboard.
Dragging a resource onto a button control
From the menu that appears, select which property of the control you want to apply the resource to.
Applying a resource to the Background property of the button
To apply a resource by using the Advanced options menu
In the properties view of the Properties panel, locate the property that you want to set to a resource.
Click Advanced options , point to Local Resource, and then select the resource name from the drop-down list that appears.
To apply a brush resource
In the properties view of the Properties panel, select the brush that you want to set to a resource.
In the Brush resources tab , select the name of the resource.
To apply a color resource
In the properties view of the Properties panel, select the brush whose color you want to set to a resource. If the brush is a Gradient brush , select the gradient stop for the color.
In the Color resources tab, select the name of the resource.
Apply a template to another control of the same type
You have many ways to apply templates to controls.
To apply a template by selecting it in the Assets panel and drawing a new control
In the Tools panel, click Assets .
In the Styles category of the Assets panel, select the template you created.
On the artboard, use your pointer to draw a bounding box.
A new control is drawn that matches the selected template, and the template is automatically applied.
To apply a template by dragging it from the Resources panel
From the Resources panel, drag a template resource onto a control on the artboard.
From the drop-down menu that appears, select the Style property.
To apply a template by using the Advanced options menu
Select the object to which you want to apply a template.
In the properties view of the Properties panel, locate the Style property.
Next to the Style property, click Advanced options , point to Local Resource, and then select the name of the template from the drop-down list that appears.
To move resources to the App.xaml file
If you did not define your resources in the App.xaml file when you created them, you can move your resources to the App.xaml file by dragging them in the Resources panel.
Suggerimento If you do not see your resources, you might have to expand the nodes under the document node (typically Page.xaml).
To copy resources to other projects
From the Projects panel, double-click the App.xaml file to open it on the artboard.
The App.xaml file cannot be viewed in Design view, so select the XAML tab on the right side of the artboard.
Resources are defined between <Application.Resources> tags.
<Application.Resources> </Application.Resources>
Between these tags, property resources are defined in tags that represent the type of property they are. The Key attribute represents the name that you gave to the resource.
<FontFamily x:Key="ApplicationFont">Segoe UI</FontFamily> <LinearGradientBrush x:Key="BorderBrush" EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="#FF000000"/> <GradientStop Color="#FFC64545" Offset="1"/> </LinearGradientBrush>
Templates are defined between <Style> tags. The Key attribute represents the name that you gave to the template.
<Style x:Key="ImageButton" TargetType="Button"> <Setter Property="Background" Value="#FF1F3B53"/> <Setter Property="Template"> ... </Setter> </Style>
Highlight the XAML that represents the resources that you want to move to another project, and then press Ctrl+C to copy them.
Open the other project in Blend, open the App.xaml file on the artboard in XAML view, insert the pointer right after the <Application.Resources> tag, and then press Ctrl+V to paste the resources.
Make sure that no key names are duplicated in any preexisting resources.
Build your project (Ctrl+Shift+B) to make sure that the new resources were copied correctly.
Suggerimento Alternatively, you could copy the complete App.xaml file into a new project and then just change the name in the x:Class attribute to the name of the new project.
<Application
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
x:Class="ProjectName.App" ...
You can watch how to create a control skin for a button and a check box in the "How Do I?" video tutorials on the Expression Community website.