VisualStateManager

管理控件狀態(tài)和管理控件狀態(tài)的轉(zhuǎn)換邏輯

<Window.Resources>
    <Style TargetType="Button" x:Key="AnimatedStyle" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <!-- Define our button's border. Note that
                    the text color is inherited by child 
                    elements, that is we give it a name, so 
                    we can target it with the animation -->
                    <Border BorderBrush="Black" BorderThickness="1" 
                    CornerRadius="2" 
                    TextBlock.Foreground="WhiteSmoke" 
                    Name="theBorder">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>

                            <!-- To match the appearance of a typical button, 
                            we use two rectangles -->
                            <Rectangle Name="topBackground" Fill="DarkGray"/>
                            <Rectangle Grid.Row="1" Name="bottomBackground" 
                                Fill="Black" />

                            <!-- The content presenter shows the 
                            button's text -->
                            <ContentPresenter Grid.RowSpan="2" 
                                VerticalAlignment="Center" 
                                HorizontalAlignment="Center" />
                        </Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup Name="CommonStates">

                                <!-- Here is where we define 
                            the disable animation -->
                                <!--控件有狀態(tài):Disabled崩哩,Normal等等-->
                                <VisualState Name="Disabled">
                                    <Storyboard>
                                        <ColorAnimation 
                                        Storyboard.TargetName="topBackground"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(Color)"
                                        To="White" Duration="0:0:.5" />
                                        <ColorAnimation 
                                        Storyboard.TargetName="bottomBackground"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(Color)"
                                        To="WhiteSmoke" Duration="0:0:0.5" />
                                        <ColorAnimation 
                                        Storyboard.TargetName="theBorder"
                                        Storyboard.TargetProperty="(TextBlock.Foreground).(Color)"
                                        To="Gray" Duration="0:0:0.5" />
                                    </Storyboard>
                                </VisualState>

                                <!-- Here is where the enabled animation 
                                is defined -->
                                <VisualState Name="Normal">
                                    <Storyboard>
                                        <ColorAnimation 
                                        Storyboard.TargetName="topBackground"
                                        Storyboard.TargetProperty="(Rectangle.Fill).Color"
                                        To="DarkGray" Duration="0:0:0.5" />
                                        <ColorAnimation 
                                        Storyboard.TargetName="bottomBackground"
                                        Storyboard.TargetProperty="(Rectangle.Fill).(Color)"
                                        To="Black" Duration="0:0:0.5" />
                                        <ColorAnimation 
                                        Storyboard.TargetName="theBorder"
                                        Storyboard.TargetProperty="(TextBlock.Foreground).Color"
                                        To="WhiteSmoke" Duration="0:0:0.5" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <CheckBox Grid.Row="0" Content="Enable Button" 
        Name="chkEnabled" IsChecked="True" VerticalAlignment="Center" />
    <!--此處通過IsEnabled來進(jìn)行Disabled,Normal二者的切換-->
    <Button Grid.Row="1" Content="Test Button" Name="btnTest" 
    IsEnabled="{Binding ElementName=chkEnabled,Path=IsChecked}" 
    Style="{StaticResource AnimatedStyle}" Click="ButtonClick"/>
</Grid>

示例代碼

https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/VisualStateManager

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市码秉,隨后出現(xiàn)的幾起案子逞力,更是在濱河造成了極大的恐慌病瞳,老刑警劉巖勤讽,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件铲敛,死亡現(xiàn)場離奇詭異待错,居然都是意外死亡籽孙,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進(jìn)店門火俄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來犯建,“玉大人,你說我怎么就攤上這事烛占√タ妫” “怎么了?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵忆家,是天一觀的道長犹菇。 經(jīng)常有香客問我,道長芽卿,這世上最難降的妖魔是什么揭芍? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮卸例,結(jié)果婚禮上称杨,老公的妹妹穿的比我還像新娘。我一直安慰自己筷转,他們只是感情好姑原,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著呜舒,像睡著了一般锭汛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天唤殴,我揣著相機(jī)與錄音般婆,去河邊找鬼。 笑死朵逝,一個(gè)胖子當(dāng)著我的面吹牛蔚袍,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播配名,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼啤咽,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了渠脉?” 一聲冷哼從身側(cè)響起闰蚕,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎连舍,沒想到半個(gè)月后没陡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡索赏,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年盼玄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片潜腻。...
    茶點(diǎn)故事閱讀 39,739評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡埃儿,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出融涣,到底是詐尸還是另有隱情童番,我是刑警寧澤,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布威鹿,位于F島的核電站剃斧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏忽你。R本人自食惡果不足惜幼东,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望科雳。 院中可真熱鬧根蟹,春花似錦、人聲如沸糟秘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽尿赚。三九已至散庶,卻和暖如春沛婴,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背督赤。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留泻蚊,地道東北人躲舌。 一個(gè)月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓,卻偏偏與公主長得像性雄,于是被迫代替她去往敵國和親没卸。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容