2011-02-03 88 views
2

這裏有幾個問題,這些問題涉及點擊時設置Button背景顏色。 以此爲解決這些問題:WP7 - 設置按鈕Background和BorderBrush除了點擊前景色

<phone:PhoneApplicationPage ...> 
    <phone:PhoneApplicationPage.Resources> 
     <Style x:Key="ButtonStyle1" TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Grid Background="Transparent"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="MouseOver"/> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ColorAnimation Duration="0" To="Cyan" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ButtonBackground" d:IsOptimized="True"/> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}" Background="Black"> 
           <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </phone:PhoneApplicationPage.Resources> 

    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Button Content="Button" Style="{StaticResource ButtonStyle1}"/> 
    </Grid> 
</phone:PhoneApplicationPage> 

我期待用這個模板也設置BorderBrush前景的顏色,但我扭捏這個XAML才結束了不好的影響。

[注:該行爲是,當我設置代碼隱藏的顏色,它們不運行我的應用程序時生效,因爲顏色是由樣式覆蓋]

回答

3

如果手動調整XAML - 你就錯了。

不要與XAML的禪戰鬥,與它流動。將Expression Blend融入到所有GUI設計的開發工作流程中,或者爲手動XAML編輯的無盡恐怖做好準備。

特別針對VisualStateManagerm手動編輯XAML絕對沒有意義,因爲它是由Silverlight團隊設計的,因此它可以在Expression Blend中得到最佳使用。

我強烈建議你花30分鐘觀看這4個「我該怎麼做?」 VSM視頻Steve White @http://expression.microsoft.com/en-us/cc643423.aspx

這4個視頻在VSM工作的初期幫助我瞭解瞭如何使用VSM以及如何將我的UI邏輯最佳地表達到視覺狀態。

在Expression Blend中獲得背景色來改變點擊很簡單,只要:

  1. 將&下降Expression Blend的一個新的按鈕。
  2. 右鍵單擊「編輯模板 - >編輯複製」。
  3. 從「狀態」窗格中選擇「按下」VSM狀態。
  4. 更改「ButtonBackground」的背景顏色。
+0

您的評論是有道理的,我已經與Blend合作過一些。我唯一的問題是,我想要做的就是調整我的Button,以便我可以在代碼隱藏中設置顏色,並且不得不使用blend來嘗試這樣做,就像使用火箭筒殺死一隻螞蟻。也許這是關於WP7/Silverlight/WPF的評論。 – pearcewg 2011-02-04 12:31:31