wpf - VisualState and Customzing the appearance...

VisualState defined with group called VisualStateGroup define some abstraction of the Visual State, which is part of the contract of A control. They defined an isolated state the control is in,in that state, the Control should have a certain appearance. such as Focused, pressed, and others. 


As I said, it is part of the control contract, and let's see the skeleton of the Button under the question. 

[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
[TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Pressed", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusStates")]
[TemplateVisualState(Name = "Focused", GroupName = "FocusStates")]
public class Button : ButtonBase
{
}


When you are styling the control, you probably want to duplicate the VisualState that a control has, such as the exaple shown in [1]  .

<ResourceDictionary     xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:ButtonControlLibrary;assembly=ButtonControlLibrary"
    xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
    <Style TargetType="src:MyButton">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="src:MyButton">
                    <vsm:VisualStateManager.VisualStateGroups>
                        <!--Define the states for the common states. The states in a 
                                    VisualStateGroup are mutually exclusive to each other.-->
                        <vsm:VisualStateGroup x:Name="CommonStates">
                            <!--Define the VisualStates in this VistualStateGroup.-->
                            <vsm:VisualState x:Name="Normal"/>
                            <vsm:VisualState x:Name="MouseOver" />
                            <vsm:VisualState x:Name="Pressed" />
                            <vsm:VisualState x:Name="Disabled" />
                        </vsm:VisualStateGroup>
                        <!--Define the states for the focus states. The states in a 
                                    VisualStateGroup are mutually exclusive to each other.-->
                        <vsm:VisualStateGroup x:Name="FocusStates">
                            <!--Define the VisualStates in this VistualStateGroup.-->
                            <vsm:VisualState x:Name="Focused" />
                            <vsm:VisualState x:Name="Unfocused" />
                        </vsm:VisualStateGroup>
                    </vsm:VisualStateManager.VisualStateGroups>
                    <!--The parts of the button control will be defined here.-->
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
and when you are filling out the details of the VisualState appearance, you probably will need to do this: 

<!--Change the border of the button to red when the
mouse is over the button.-->
        <vsm:VisualState x:Name="MouseOver">
            <Storyboard>
                <ColorAnimation Storyboard.TargetName="BorderBrush" 
                Storyboard.TargetProperty="Color" To="Red" />

            </Storyboard>
        </vsm:VisualState>

To make this happen, you might start by modifying some existing ControlTemplate, you can use my Utilities class or you can directly get the ControlTemplate from MSDN, as is shown in reference list [2].

In this post, the following article is been referenced. 

[1] Walkthrough: Customizing the Appearance of a Button by Using a ControlTemplate

[2] Button Styles and Templates

[3] Customizing the Appearance of an Existing Control by Using a ControlTemplate

转载于:https://my.oschina.net/u/854138/blog/130840

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值