Define the control template as a resource and use StaticResource to refer to the resource
1 < Margin= "Ten" Padding= "5" Template= "{ StaticResource buttontemplaate}">2 a simple Button with A Custom Template</Button>
The basic framework for the control template is as follows:
1 <Window.resource>2 <ControlTemplatex:key= "Buttontemplate"Target="{x:type Button}">3 ...4 </ControlTemplate>5 </Window.resource>
2. Simple button
WPF uses template bindings to extract a value from the controls that are applied to the template.
1 <Window.resource>2 <ControlTemplatex:key= "Buttontemplate"Target="{x:type Button}">3 <BorderBorderBrush= "Orange"borderthickness= "3"Cornerradius= "2"4 Background= "Red"Textblock.foreground= "White">5 <ContentPresenterRecognizesaccesskey= "True"Margin="{TemplateBinding Padding}"></ContentPresenter>6 </ControlTemplate>7 </Window.resource>
Template bindings support the WPF Change monitoring infrastructure, which is included in all dependency properties. This means that if you modify the properties of the control, the template automatically considers the change. This detail is especially useful when using animations that repeatedly change property values over a short period of time.
To add an event-handling method to a control:
1 <ControlTemplatex:key= "Buttontemplate"TargetType="{x:type Button}">2 <BorderName= "Border"BorderBrush= "Orange" .... >3 </Border>4 <controltemplate.triggers>5 <Trigger Property= "IsMouseOver"Value= "True">6 <SetterTargetName= "Border" Property= "Background"Value= "Darkred" />7 </Trigger>8 /*9 * More events can be addedTen */ One </controltemplate.triggers> A </ControlTemplate>
Note that for disabling features, it is generally at the end, defined at the end of the list of triggers, to ensure that the IsEnabled property trigger has the highest precedence
1 < trigger property = "IsEnabled" Value = "False" > 2 </ Setter targetname= "Border" property= "Textblock.foreground" value= "Gray"/ > 3 </ setter targetname= "Border" property= "Textblock.background" value= "mistyrose"/ > 4 </ Trigger >
3. Triggers that apply animations
1 <controltemplate.triggers>2 <EventTriggerRoutedEvent= "MouseEnter">3 <BeginStoryboard>4 <Storyboard>5 <ColorAnimationStoryboard.TargetName= "Border"Storyboard.TargetProperty= "Background.color"6 to= "Blue"Duration= "0:0:1"AutoReverse= "True"RepeatBehavior= "Forever"></ColorAnimation>7 </Storyboard>8 </BeginStoryboard>9 </EventTrigger>Ten <EventTriggerRoutedEvent= "MouseLeave"> One <BeginStoryboard> A <Storyboard> - <ColorAnimationStoryboard.TargetName= "Border"Storyboard.TargetProperty= "Background.color"Duration= "0:0:0.5">
</ColorAnimation> - </Storyboard> the </BeginStoryboard> - </EventTrigger> - <Trigger Property= "IsPressed"Value= "True"> - <SetterTargetName= "Border" Property= "Background"Value= "Indianred" /> + <SetterTargetName= "Border" Property= "BorderBrush"Value= "Darkkhaki" /> - </Trigger> + <Trigger Property= "iskeyboardfocused"Value= "True"> A <SetterTargetName= "Focuscue" Property= "Visibility"Value= "Visible" /> at </Trigger> - </controltemplate.triggers>
4. Modifying resources through code
1 New ResourceDictionary (); 2 New Uri (3 " Resources/gradientbuttonvariant.xaml " , urikind.relative); 4 this. resources.mergeddictionaries[0
WPF Programming Bible control Template (vii)