日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

【W(wǎng)PF學(xué)習(xí)】第六十六章 支持可視化狀態(tài)

 怡紅公子0526 2021-04-04

  上一章介紹的ColorPicker控件,是控件設(shè)計(jì)的最好示例。因?yàn)槠湫袨楹涂梢暬庥^是精心分離的,所以其他設(shè)計(jì)人員可開發(fā)動(dòng)態(tài)改變其外觀的新模板。

  ColorPicker控件如此簡(jiǎn)單的一個(gè)原因是不涉及狀態(tài)。換句話說,不根據(jù)是否具有焦點(diǎn)、鼠標(biāo)是否在它上面懸停、是否禁用等狀態(tài)區(qū)分其可視化外觀。接下來本章介紹的FlipPanel自定義控件有些不同。

  FlipPanel控件背后的基本思想是,為駐留內(nèi)容提供兩個(gè)表面,但每次只有一個(gè)表面是可見的。為看到其他內(nèi)容,需要在兩個(gè)表面之間進(jìn)行“翻轉(zhuǎn)”??赏ㄟ^控件模板定制翻轉(zhuǎn)效果,但默認(rèn)效果使用在前面和后面之間進(jìn)行過渡的淡化效果。根據(jù)應(yīng)用程序,可以使用FlipPanel控件把數(shù)據(jù)條目表單與一些由幫助的文檔組合起來,以便為相同的數(shù)據(jù)提供一個(gè)簡(jiǎn)單或較復(fù)雜的試圖,或在一個(gè)簡(jiǎn)單游戲中將問題和答案融合在一起。

  可通過代碼執(zhí)行翻轉(zhuǎn)(通過設(shè)置名為IsFlipped的屬性),也可使用一個(gè)便捷的按鈕來翻轉(zhuǎn)面板(除非控件使用這從模板中移除了該按鈕)。

  顯然,控件模板需要制定兩個(gè)獨(dú)立部分:FlipPanel控件的前后內(nèi)容區(qū)域。然而,還有一個(gè)細(xì)節(jié)——FlipPanel控件需要一種方法在兩個(gè)狀態(tài)之間進(jìn)行切換:翻轉(zhuǎn)過的狀態(tài)與未翻轉(zhuǎn)過的狀態(tài)??赏ㄟ^為模板添加觸發(fā)器完成該工作。當(dāng)單擊按鈕是,可使用一個(gè)觸發(fā)器隱藏前面的面板并顯示第二個(gè)面板,而使用另一個(gè)觸發(fā)器翻轉(zhuǎn)這些更改。這兩個(gè)觸發(fā)器都可以使用喜歡的任何動(dòng)畫。但通過使用可視化狀態(tài),可向控件使用這清晰地指明這兩個(gè)狀態(tài)是模板的必須部分,不是為適當(dāng)?shù)膶傩曰蚴录帉懹|發(fā)器,控件使用能管著只需要填充適當(dāng)?shù)臓顟B(tài)動(dòng)畫。如果使用Expression Blend,該任務(wù)甚至變得更簡(jiǎn)單。

一、開始編寫FlipPanel類

  FlipPanel的基本骨架非常簡(jiǎn)單。包含用戶可用單一元素(最有可能是包含各種元素的布局容器)填充的兩個(gè)內(nèi)容區(qū)域。從技術(shù)角度看,這意味著FlipPanel控件不是真正的面板,因?yàn)椴荒苁褂貌季诌壿嫿M織一組子元素。然而,這不會(huì)造成問題。因?yàn)镕lipPanel控件的結(jié)構(gòu)是清晰直觀的。FlipPanel控件還包含一個(gè)翻轉(zhuǎn)按鈕,用戶可使用該按鈕在兩個(gè)不同的內(nèi)容區(qū)域之間進(jìn)行切換。

  盡管可通過繼承自ContentControl或Panel等控件類來創(chuàng)建自定義控件,但是FlipPanel直接繼承自Control基類。如果不需要特定控件類的功能,這是最好的起點(diǎn)。不應(yīng)該當(dāng)繼承自更簡(jiǎn)單的FrameworkElement類,除非希望創(chuàng)建不使用標(biāo)準(zhǔn)控件和模板基礎(chǔ)框架的元素:

public class FlipPanel:Control
    {

    }

  首先為FlipPanel類創(chuàng)建屬性。與WPF元素中的幾乎所有屬性一樣,應(yīng)使用依賴項(xiàng)屬性。以下代碼演示了FlipPanel如何定義FrontContent屬性,該屬性保持在前表面上顯示的元素。

public static readonly DependencyProperty FrontContentProperty =
            DependencyProperty.Register("FrontContent", typeof(object), typeof(FlipPanel), null);

  接著需要調(diào)用基類的GetValue()和SetValue()方法的常規(guī).NET屬性過程,以便修改依賴性屬性。下面是FrontContent屬性的實(shí)現(xiàn)過程:

 /// <summary>
/// 前面內(nèi)容
/// </summary>
public object FrontContent
{
   get { return GetValue(FrontContentProperty); }
   set { SetValue(FrontContentProperty, value); }
}

  同理,還需要一個(gè)存儲(chǔ)背面的依賴項(xiàng)屬性。如下所示:

public static readonly DependencyProperty BackContentProperty =
            DependencyProperty.Register("BackContent", typeof(object), typeof(FlipPanel), null);

        /// <summary>
        /// 背面內(nèi)容
        /// </summary>
        public object BackContent
        {
            get { return GetValue(BackContentProperty); }
            set { SetValue(BackContentProperty, value); }
        }

  還需要添加一個(gè)重要屬性:IsFlipped。這個(gè)Boolean類型的屬性持續(xù)跟蹤FlipPanel控件的當(dāng)前狀態(tài)(面向前面還是面向后面),使控件使用者能夠通過編程翻轉(zhuǎn)狀態(tài):

public static readonly DependencyProperty IsFlippedProperty =
            DependencyProperty.Register("IsFlipped", typeof(bool), typeof(FlipPanel), null);

        /// <summary>
        /// 是否翻轉(zhuǎn)
        /// </summary>
        public bool IsFlipped
        {
            get { return (bool)GetValue(IsFlippedProperty); }
            set { 
                SetValue(IsFlippedProperty, value);
                ChangeVisualState(true);
            }
        }

  IsFlipped屬性設(shè)置器調(diào)用自定義方法ChangeVisualState()。該方法確保更新顯示以匹配當(dāng)前的翻轉(zhuǎn)狀態(tài)。稍后介紹ChangeVisualState方法。

  FlipPanel類不需要更多屬性,因?yàn)樗鼘?shí)際上從Control類繼承了它所需要的幾乎所有內(nèi)容。一個(gè)例外是CornerRadius屬性。盡管Control類包含了BorderBrush和BorderThickness屬性,可以使用這些屬性在FlipPanel控件上繪制邊框,但缺少將方形邊緣變成光滑曲線的CornerRadius屬性,如Border元素所做的那樣。在FlipPanel控件中實(shí)現(xiàn)類似的效果很容易,前提是添加CornerRadius依賴性屬性并使用該屬性配置FlipPanel控件的默認(rèn)控件模板中的Border元素:

public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(FlipPanel), null);

        /// <summary>
        /// 控件邊框圓角
        /// </summary>
        public CornerRadius CornerRadius
        {
            get
            {
                return (CornerRadius)GetValue(CornerRadiusProperty);
            }
            set
            {
                SetValue(CornerRadiusProperty, value);
            }
        }

  還需要為FlipPanel控件添加一個(gè)應(yīng)用默認(rèn)模板的樣式。將該樣式放在generic.xaml資源字典中,正如在開發(fā)ColorPicker控件時(shí)所做的那樣。下面是需要的基本骨架:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CustomControls">
    <Style TargetType="{x:Type local:FlipPanel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:FlipPanel">
                    ...
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

  還有最后一個(gè)細(xì)節(jié)。為通知控件從generic.xaml文件獲取默認(rèn)樣式,需要在FlipPanel類的靜態(tài)構(gòu)造函數(shù)中調(diào)用DefaultStyleKeyProperty.OverrideMetadata()方法:

DefaultStyleKeyProperty.OverrideMetadata(typeof(FlipPanel), new FrameworkPropertyMetadata(typeof(FlipPanel)));

二、選擇部件和狀態(tài)

  現(xiàn)在已經(jīng)具備了基本結(jié)構(gòu),并且已經(jīng)準(zhǔn)備好確定將在控件模板中使用的部件和狀態(tài)了。

  顯然,F(xiàn)lipPanel需要兩個(gè)狀態(tài):

  •   正常狀態(tài)。該故事板確保只有前面的內(nèi)容是可見的,后面的內(nèi)容被翻轉(zhuǎn)、淡化或移出試圖。
  •   翻轉(zhuǎn)狀態(tài)。該故事板確保只有后面的內(nèi)容是可見的,前面的內(nèi)容通過動(dòng)畫被移出試圖。

  此外,需要兩個(gè)部件:

  •   FlipButton。這是一個(gè)按鈕,當(dāng)單擊該按鈕時(shí),將試圖從前面改到后面(或從后面改到前面)。FlipPanel控件通過處理該按鈕的事件提供該服務(wù)。
  •   FlipButtonAlternate。這是一個(gè)可選元素,與FlipButton的工作方式相同。允許控件使用者在自定義模板中使用兩種不同的方法。一種選擇是使用在可翻轉(zhuǎn)區(qū)域外的單個(gè)翻轉(zhuǎn)按鈕、另一種選擇是在可翻轉(zhuǎn)區(qū)域的面板兩側(cè)放置獨(dú)立的翻轉(zhuǎn)按鈕。

  還應(yīng)當(dāng)為前后內(nèi)容區(qū)域添加部件。然而,F(xiàn)lipPanel克難攻堅(jiān)不需要直接操作這些區(qū)域,只要模板包含在適當(dāng)?shù)臅r(shí)間隱藏和顯示它們的動(dòng)畫即可(另一種選擇是定義這些部件,從而可以明確地使用代碼改變它們的可見性。這樣一來,即使沒有定義動(dòng)畫,通過隱藏一部分并顯示另一部分,面板仍能在前后內(nèi)容區(qū)域之間變化。為簡(jiǎn)單起見,F(xiàn)lipPanel沒有采取這種選擇)。

  為表面FlipPanel使用這些部件和狀態(tài)的事實(shí),應(yīng)為自定義控件類應(yīng)用TemplatePart特性,如下所示:

    [TemplatePart(Name = "FlipButton", Type = typeof(ToggleButton))]
    [TemplatePart(Name = "FlipButtonAlternate", Type = typeof(ToggleButton))]
    [TemplateVisualState(Name = "Normal", GroupName = "ViewStates")]
    [TemplateVisualState(Name = "Flipped", GroupName = "ViewStates")]
    public class FlipPanel : Control
    {

    }

三、默認(rèn)控件模板

  現(xiàn)在,可將這些內(nèi)容投入到默認(rèn)控件模板中。根元素是具有兩行的Grid面板,該面板包含內(nèi)容區(qū)域(在頂行)和翻轉(zhuǎn)按鈕(在底行)。用兩個(gè)相互重疊的Border元素填充內(nèi)容區(qū)域,代表前面和后面的內(nèi)容,但一次只顯示前面和后面的內(nèi)容。

  為了填充前面和后面的內(nèi)容區(qū)域,F(xiàn)lipPanel控件使用ContentControl元素。該技術(shù)幾乎和自定義按鈕示例相同,只是需要兩個(gè)ContentPresenter元素,分別用于FlipPanel控件的前面和后面。FlipPanel控件還包含獨(dú)立的Border元素來封裝每個(gè)ContentPresenter元素。從而讓控件使用者能通過設(shè)置FlipPanel的幾個(gè)直接屬性勾勒出可翻轉(zhuǎn)內(nèi)容區(qū)域(BorderBrush、BorderThickness、Background以及CornerRadius),而不是強(qiáng)制性地手動(dòng)添加邊框。

  下面是默認(rèn)控件模板的基本骨架:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:CustomControls">
    <Style TargetType="{x:Type local:FlipPanel}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:FlipPanel}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                        </Grid.RowDefinitions>

                        <!-- This is the front content. -->
                        <Border x:Name="FrontContent" BorderBrush="{TemplateBinding BorderBrush}"
               BorderThickness="{TemplateBinding BorderThickness}"
               CornerRadius="{TemplateBinding CornerRadius}"
               >
                            <ContentPresenter
                     Content="{TemplateBinding FrontContent}">
                            </ContentPresenter>
                        </Border>

                        <!-- This is the back content. -->
                        <Border x:Name="BackContent" BorderBrush="{TemplateBinding BorderBrush}"
           BorderThickness="{TemplateBinding BorderThickness}"
           CornerRadius="{TemplateBinding CornerRadius}"
           >
                            <ContentPresenter
                     Content="{TemplateBinding BackContent}">
                            </ContentPresenter>
                        </Border>

                        <!-- This the flip button. -->
                        <ToggleButton Grid.Row="1" x:Name="FlipButton" 
                     Margin="0,10,0,0" >
                        </ToggleButton>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

  當(dāng)創(chuàng)建默認(rèn)控件模板時(shí),最好避免硬編碼控件使用者可能希望定制的細(xì)節(jié)。相反,需要使用模板綁定表達(dá)式。在這個(gè)示例中,使用模板綁定表達(dá)式設(shè)置了幾個(gè)屬性:BorderBrush、BorderThickness、CornerRadius、Background、FrontContent以及BackContent。為設(shè)置這些屬性的默認(rèn)值(這樣即使控件使用者沒有設(shè)置它們,也仍然確保能得到正確的可視化外觀),必須為控件的默認(rèn)樣式添加額外的設(shè)置器。

  1、翻轉(zhuǎn)按鈕

  在上面的示例中,顯示的控件模板包含一個(gè)ToggleButton按鈕。然而,該按鈕使用ToggleButton的默認(rèn)外觀,這使得ToggleButton按鈕看似普遍的按鈕,完全具有傳統(tǒng)的陰影背景。這對(duì)于FlipPanel控件是不合適的。

  盡管可替換ToggleButton中的任何內(nèi)容,但FlipPanel需要進(jìn)一步。它需要去掉標(biāo)準(zhǔn)的背景并根據(jù)ToggleButton按鈕的狀態(tài)改變其內(nèi)部元素的外觀。

  為創(chuàng)建這種效果,需要為ToggleButton設(shè)置自定義控件模板。該控件模板能夠包含繪制所需箭頭的形狀元素。在該例中,ToggleButton是使用用于繪制圓的Ellipse元素和用于繪制箭頭的Path元素繪制的,這兩個(gè)元素都放在具有單個(gè)單元格的Grid面板中,以及需要一個(gè)改變箭頭指向的RotateTransform對(duì)象:

<ToggleButton Grid.Row="1" x:Name="FlipButton" RenderTransformOrigin="0.5,0.5"
                     Margin="0,10,0,0" Width="19" Height="19">
                            <ToggleButton.Template>
                                <ControlTemplate>
                                    <Grid>
                                        <Ellipse Stroke="#FFA9A9A9"  Fill="AliceBlue"  />
                                        <Path Data="M1,1.5L4.5,5 8,1.5"
                             Stroke="#FF666666" StrokeThickness="2"
                             HorizontalAlignment="Center" VerticalAlignment="Center">
                                        </Path>
                                    </Grid>
                                </ControlTemplate>
                            </ToggleButton.Template>
                            <ToggleButton.RenderTransform>
                                <RotateTransform x:Name="FlipButtonTransform" Angle="-90"></RotateTransform>
                            </ToggleButton.RenderTransform>
                        </ToggleButton>

  2、定義狀態(tài)動(dòng)畫

  狀態(tài)動(dòng)畫是控件模板中最有趣的部分。它們是提供翻轉(zhuǎn)行為的要素,它們還是為FlipPanel創(chuàng)建自定義模板的開發(fā)人員最有可能修改的細(xì)節(jié)。

  為定義狀態(tài)組,必須在控制模板的根元素中添加VisualStateManager.VisualStateGroups元素,如下所示:

 <ControlTemplate TargetType="{x:Type local:FlipPanel}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            ...
                        </VisualStateManager.VisualStateGroups>
                    </Grid>
</ControlTemplate>

  可在VisualStateGroups元素內(nèi)部使用具有合適名稱的VisualStateGroup元素創(chuàng)建狀態(tài)組。在每個(gè)VisualStateGroup元素內(nèi)部,為每個(gè)狀態(tài)添加一個(gè)VisualState元素。對(duì)于FlipPanel面板,有一個(gè)包含兩個(gè)可視化狀態(tài)的組:

<VisualStateManager.VisualStateGroups>
   <VisualStateGroup x:Name="ViewStates">
       <VisualState x:Name="Normal">
            <Storyboard>
               ...
            </Storyboard>
       </VisualState>
   </VisualStateGroup>
   <VisualStateGroup x:Name="FocusStates">
       <VisualState x:Name="Flipped">
            <Storyboard>
               ...
            </Storyboard>
       </VisualState>
   </VisualStateGroup>
</VisualStateManager.VisualStateGroups>

  每個(gè)狀態(tài)對(duì)應(yīng)一個(gè)具有一個(gè)或多個(gè)動(dòng)畫的故事板。如果存在這些故事板,就會(huì)在適當(dāng)?shù)臅r(shí)機(jī)觸發(fā)它們(如果不存在,控件將按正常方式降級(jí),而不會(huì)引發(fā)錯(cuò)誤)。

  在默認(rèn)控件模板中,動(dòng)畫使用簡(jiǎn)單的淡化效果從一個(gè)內(nèi)容區(qū)域改變到另一個(gè)內(nèi)容區(qū)域,并使用旋轉(zhuǎn)變換翻轉(zhuǎn)ToggleButton箭頭使其指向另一個(gè)方向。下面是完成這兩個(gè)任務(wù)的標(biāo)記:

<VisualState x:Name="Normal">
     <Storyboard>
           <DoubleAnimation Storyboard.TargetName="BackContent" 
                       Storyboard.TargetProperty="Opacity" To="0" Duration="0" ></DoubleAnimation>
     </Storyboard>
</VisualState>

<VisualState x:Name="Flipped">
    <Storyboard>
        <DoubleAnimation Storyboard.TargetName="FlipButtonTransform"
       Storyboard.TargetProperty="Angle" To="90" Duration="0">    
        </DoubleAnimation>
     <DoubleAnimation Storyboard.TargetName="FrontContent" 
                       Storyboard.TargetProperty="Opacity" To="0" Duration="0"></DoubleAnimation>
    </Storyboard>
</VisualState>

  通過上面標(biāo)記,發(fā)現(xiàn)可視化狀態(tài)持續(xù)時(shí)間設(shè)置為0,這意味著動(dòng)畫立即應(yīng)用其效果。這看起來可能有些怪——畢竟,不是需要更平緩的改變從而能夠注意到動(dòng)畫效果嗎?

  時(shí)機(jī)上,該設(shè)計(jì)完成正確,因?yàn)榭梢暬癄顟B(tài)用于表示控件在適當(dāng)狀態(tài)時(shí)的外觀。例如,當(dāng)翻轉(zhuǎn)面板處于翻轉(zhuǎn)過的狀態(tài)是,簡(jiǎn)單地顯示其背面內(nèi)容。翻轉(zhuǎn)過程是在FlipPanel控件進(jìn)入翻轉(zhuǎn)狀態(tài)前得過渡,而不是翻轉(zhuǎn)狀態(tài)本身的一部分。

  3、定義狀態(tài)過渡

  過渡是從當(dāng)前狀態(tài)到新狀態(tài)的動(dòng)畫。變換模型的優(yōu)點(diǎn)之一是不需要為動(dòng)畫創(chuàng)建故事板。例如,如果添加如下標(biāo)記,WPF會(huì)創(chuàng)建持續(xù)時(shí)間為0.7秒得動(dòng)畫以改變FlipPanel控件的透明度,從而創(chuàng)建所希望的悅目的褪色效果:

 <VisualStateGroup x:Name="ViewStates">
      <VisualStateGroup.Transitions>
           <VisualTransition GeneratedDuration="0:0:0.7">    
           </VisualTransition>
      </VisualStateGroup.Transitions>
      <VisualState x:Name="Normal">
       ...
      </VisualState>
</VisualStateGroup>

  過渡會(huì)應(yīng)用到狀態(tài)組,當(dāng)定義過渡時(shí),必須將其添加到VisualStateGroup.Transitions集合。這個(gè)示例使用最簡(jiǎn)單的過渡類型:默認(rèn)過渡。默認(rèn)過渡應(yīng)用于該組中的所有狀態(tài)變化。

  默認(rèn)過渡是很方便的,但用于所有情況的解決方案不可能總是適合的。例如,可能希望FlipPanel控件根據(jù)其進(jìn)入的狀態(tài)以不同的速度過渡。為實(shí)現(xiàn)該效果,需要定義多個(gè)過渡,并且需要設(shè)置To屬性以指示何時(shí)應(yīng)用過渡效果。

  例如,如果有以下過渡:

<VisualStateGroup.Transitions>
       <VisualTransition To="Flipped" GeneratedDuration="0:0:0.5"></VisualTransition>
       <VisualTransition To="Normal" GeneratedDuration="0:0:0.1"></VisualTransition>
</VisualStateGroup.Transitions>

  FlipPanel將在0.5秒得時(shí)間內(nèi)切換到Flipped狀態(tài),并在0.1秒得時(shí)間內(nèi)進(jìn)入Normal狀態(tài)。

  這個(gè)示例顯示了當(dāng)進(jìn)入特定狀態(tài)時(shí)應(yīng)用的過渡,但還可使用From屬性創(chuàng)建當(dāng)離開某個(gè)狀態(tài)時(shí)應(yīng)用的過渡,并且可結(jié)合使用To和From屬性來創(chuàng)建更特殊的只有當(dāng)在特定的兩個(gè)狀態(tài)之間移動(dòng)時(shí)才會(huì)應(yīng)用的過渡。當(dāng)應(yīng)用過渡時(shí)WPF遍歷過渡集合,在所有應(yīng)用的過渡中查找最特殊的過渡,并只使用最特殊的那個(gè)過渡。

  為進(jìn)一步加以控制,可創(chuàng)建自定義過渡動(dòng)畫來替換WPF通常使用的自動(dòng)生成的過渡??赡軙?huì)猶由于幾個(gè)原因而創(chuàng)建自定義過渡。下面是一些例子:使用更復(fù)雜的動(dòng)畫控制動(dòng)畫的步長(zhǎng),使用動(dòng)畫緩動(dòng)、連續(xù)運(yùn)行幾個(gè)動(dòng)畫或在運(yùn)行動(dòng)畫時(shí)播放聲音。

  為定義自定義過渡,在VisualTransition元素中放置具有一個(gè)或多個(gè)動(dòng)畫的故事板。在FlipPanel示例中,可使用自定義過渡確保ToggleButton箭頭更快遞旋轉(zhuǎn)自身,而淡化過程更緩慢:

<VisualStateGroup.Transitions>
                                    <VisualTransition GeneratedDuration="0:0:0.7" To="Flipped">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="FlipButtonTransform"
       Storyboard.TargetProperty="Angle" To="90" Duration="0:0:0.2"></DoubleAnimation>
                                        </Storyboard>
                                    </VisualTransition>
                                    <VisualTransition GeneratedDuration="0:0:0.7" To="Normal">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="FlipButtonTransform"
       Storyboard.TargetProperty="Angle" To="-90" Duration="0:0:0.2"></DoubleAnimation>
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>

  但許多控件需要自定義過渡,而且編寫自定義過渡是非常乏味的工作。仍需保持零長(zhǎng)度的狀態(tài)動(dòng)畫,這還會(huì)不可避免地再可視化狀態(tài)和過渡之間復(fù)制一些細(xì)節(jié)。

  4、關(guān)聯(lián)元素

  通過上面的操作,已經(jīng)創(chuàng)建了一個(gè)相當(dāng)好的控件模板,需要在FlipPanel控件中添加一些內(nèi)容以使該模板工作。

  訣竅是使用OnApplyTemplate()方法,該方法還款用于在ColorPicker控件中設(shè)置綁定。對(duì)于FlipPanel控件,OnApplyTemplate()方法用于為FlipButton和FlipButtonAlternate部件檢索ToggleButton,并為每個(gè)部件關(guān)聯(lián)事件處理程序,從而當(dāng)用戶單擊以翻轉(zhuǎn)控件時(shí)能夠進(jìn)行響應(yīng)。最后,OnApplyTemplate()方法調(diào)用名為ChangeVisualState()的自定義方法,該方法確??丶目梢暬庥^和其當(dāng)前狀態(tài)的相匹配:

public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ToggleButton flipButton = base.GetTemplateChild("FlipButton") as ToggleButton;
            if (flipButton != null) flipButton.Click += flipButton_Click;

            // Allow for two flip buttons if needed (one for each side of the panel).
            // This is an optional design, as the control consumer may use template
            // that places the flip button outside of the panel sides, like the 
            // default template does.
            ToggleButton flipButtonAlternate = base.GetTemplateChild("FlipButtonAlternate") as ToggleButton;
            if (flipButtonAlternate != null)
                flipButtonAlternate.Click += flipButton_Click;

            this.ChangeVisualState(false);
        }

  下面是非常簡(jiǎn)單的允許用戶單擊ToggleButton按鈕并翻轉(zhuǎn)面板的事件處理程序:

private void flipButton_Click(object sender, RoutedEventArgs e)
        {
            this.IsFlipped = !this.IsFlipped;
        }

  幸運(yùn)的是,不需要手動(dòng)觸發(fā)狀態(tài)動(dòng)畫。即不需要?jiǎng)?chuàng)建也不需要觸發(fā)過渡動(dòng)畫。相反,為從一個(gè)狀態(tài)改變到另一個(gè)狀態(tài),只需要調(diào)用靜態(tài)方法VisualStateManager.GoToState()。當(dāng)調(diào)用該方法時(shí),傳遞正在改變狀態(tài)的控件對(duì)象的引用、新狀態(tài)的名稱以及確定是否顯示過渡的Boolean值。如果是由用戶引發(fā)的改變(例如,當(dāng)用戶單擊ToggleButton按鈕時(shí)),該值應(yīng)當(dāng)為true;如果是由屬性設(shè)置引發(fā)的改變(例如,如果使用頁面的標(biāo)記設(shè)置IsFlipped屬性的初始值),該值為false。

  處理控件支持的所有不同狀態(tài)可能會(huì)變得凌亂。為避免在整個(gè)控件代碼中分散調(diào)用GoToState()方法,大多數(shù)控件添加了與在FlipPanel控件中添加的ChangeVisualState()類似地方法。該方法負(fù)責(zé)應(yīng)用每個(gè)狀態(tài)組中的正確狀態(tài)。該方法中的代碼使用If語句塊(或switch語句)應(yīng)用每個(gè)狀態(tài)組的當(dāng)前狀態(tài)。該方法之所以可行,是因?yàn)樗耆梢允褂卯?dāng)前狀態(tài)的名稱調(diào)用GoToState()方法。在這種情況下,如果當(dāng)前狀態(tài)和請(qǐng)求的狀態(tài)相同,那么什么也不會(huì)發(fā)生。

  下面是用于FlipPanel控件的ChangeVisualState()方法:

private void ChangeVisualState(bool useTransitions)
{
            if (!this.IsFlipped)
            {
                VisualStateManager.GoToState(this, "Normal", useTransitions);
            }
            else
            {
                VisualStateManager.GoToState(this, "Flipped", useTransitions);
            }
}

  通常在以下位置調(diào)用ChangeVisualState()方法或其等效的方法:

  •   在OnApplyTemplate()方法的結(jié)尾,在初始化控件之后。
  •   在響應(yīng)代表狀態(tài)變化的事件時(shí),例如鼠標(biāo)移動(dòng)或單擊ToggleButton按鈕。
  •   當(dāng)響應(yīng)屬性改變或通過代碼觸發(fā)方法時(shí)(例如,IsFlipped屬性設(shè)置器調(diào)用ChangEVisualState()方法并且總是提供true,所以顯示過渡動(dòng)畫。如果希望為控件使用者提供不顯示過渡的機(jī)會(huì),可添加Flip()方法,該方法接受與為ChangeVisualState()方法傳遞的相同的Boolean參數(shù))。

  正如上面介紹的,F(xiàn)lipPanel控件非常靈活。例如,可使用該控件并且不使用ToggleButton按鈕,通過代碼進(jìn)行翻轉(zhuǎn)(可能是當(dāng)用戶單擊不同的控件時(shí))。也可在控件模板中包含一兩個(gè)翻轉(zhuǎn)按鈕,并且允許用戶進(jìn)行控制。

四、使用FlipPanel控件

  使用FlipPanel控件相對(duì)簡(jiǎn)單。標(biāo)記如下所示:

<Window x:Class="CustomControlsClient.FlipPanelTest"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="FlipPanelTest" Height="300" Width="300" 
        xmlns:lib="clr-namespace:CustomControls;assembly=CustomControls" >
    <Grid x:Name="LayoutRoot" Background="White">
        <lib:FlipPanel x:Name="panel" BorderBrush="DarkOrange" BorderThickness="3" IsFlipped="True"
         CornerRadius="4" Margin="10">
            <lib:FlipPanel.FrontContent>
                <StackPanel Margin="6">
                    <TextBlock TextWrapping="Wrap" Margin="3" FontSize="16" Foreground="DarkOrange">This is the front side of the FlipPanel.</TextBlock>
                    <Button Margin="3" Padding="3" Content="Button One"></Button>
                    <Button Margin="3" Padding="3" Content="Button Two"></Button>
                    <Button Margin="3" Padding="3" Content="Button Three"></Button>
                    <Button Margin="3" Padding="3" Content="Button Four"></Button>
                </StackPanel>
            </lib:FlipPanel.FrontContent>
            <lib:FlipPanel.BackContent>
                <Grid Margin="6">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock TextWrapping="Wrap" Margin="3" FontSize="16" Foreground="DarkMagenta">This is the back side of the FlipPanel.</TextBlock>
                    <Button Grid.Row="2" Margin="3" Padding="10" Content="Flip Back to Front" HorizontalAlignment="Center" VerticalAlignment="Center" Click="cmdFlip_Click"></Button>
                </Grid>
            </lib:FlipPanel.BackContent>
        </lib:FlipPanel>
    </Grid>
</Window>

  當(dāng)單擊FlipPanel背面的按鈕時(shí),通過編程翻轉(zhuǎn)面板:

private void cmdFlip_Click(object sender, RoutedEventArgs e)
        {
            panel.IsFlipped = !panel.IsFlipped;
        }

 本實(shí)例源碼:FlipPanel.zip

  

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多