2011-01-19 116 views
4

在WP7中,無論何時點擊一個按鈕,按鈕的背景變成白色,表明該按鈕被點擊並在釋放鼠標按鈕時變爲正常。但是我想將按鈕選擇的顏色從白色更改爲橙​​色。我可以如何點擊它?如何在WP7中單擊時更改按鈕的顏色?

+0

是否要設置切換按鈕的樣式? – 2011-01-19 05:17:41

+0

我希望單擊按鈕的顏色變爲橙色,並且在單擊鼠標按鈕後再次釋放時會恢復正常。 – Shaireen 2011-01-19 05:21:49

+0

我不可能在沒有XAML的情況下以編程方式在C#中進行編程? – 2011-01-19 16:21:44

回答

13

你將不得不與ButtonVisual States玩控制。以下是從Expression Blend提取Button默認樣式:

<Style x:Key="ButtonStyle1" TargetType="Button"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/> 
    <Setter Property="Padding" Value="10,3,10,5"/> 
    <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="Background" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
            </ObjectAnimationUsingKeyFrames> 
           </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> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
             <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
         <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

你需要改變Pressed可視狀態:

(在這種情況下,背景改爲Red當按下按鈕)

<VisualState x:Name="Pressed"> 
    <Storyboard> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/> 
     </ObjectAnimationUsingKeyFrames> 
     <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
      <DiscreteObjectKeyFrame KeyTime="0" Value="Red"/> 
     </ObjectAnimationUsingKeyFrames> 
    </Storyboard> 
</VisualState> 
0

您可以通過重新設計混合中的按鈕控件來實現此目的。

  • R單擊控制
  • 編輯模板
  • 編輯副本
  • 修改構成組件的格式進行選擇視圖狀態
11

如果您正在開發Windows手機,Blend是免費的,它是一個非常強大的功能。我甚至會說強制性的,我強烈建議你使用它。在Blend中,編輯「按下」狀態風格很簡單。

  1. 在Visual Studio中,在解決方案資源管理器中,右鍵單擊您的XAML頁面,選擇「在Expression Blend中打開」
  2. 右鍵點擊按鈕 - >編輯模板 - >創建副本(只需選擇默認值爲保存對話)

  3. 點擊「狀態」選項卡:(你會看到所有的不同的視覺狀態的列表按鈕可以有)

  4. 選擇「按下」狀態和編輯客場視覺上你的熱量內容。

你完成了!

比較,試圖編輯原始XAML,就像你將不得不在VS.

相關問題