2013-09-29 105 views
1

目前我需要在我的應用程序中放置一個「後退」按鈕。這不是要替換硬件後退按鈕,而是要返回到物品的先前狀態,如果用戶不希望繼續他或她對物品的更改。目前,我的MainPage中有以下xaml,它綁定圖像以及將「後退」按鈕放置在視圖上。如何更改按鈕的前景和背景當按下和發佈

MainPage.xaml中

<Grid x:Name="MainPageGrid" Margin="{Binding}"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width=".2*"/> 
        <ColumnDefinition Width=".2*"/> 
        <ColumnDefinition Width=".2*"/> 
        <ColumnDefinition Width=".2*"/> 
        <ColumnDefinition Width=".2*"/> 
       </Grid.ColumnDefinitions> 
       <Grid.RowDefinitions> 
        <RowDefinition Height=".2*"/> 
        <RowDefinition Height="*"/> 
       </Grid.RowDefinitions> 
       <Image x:Name="currentPhoto" Grid.ColumnSpan="5" Grid.RowSpan="2" 
         Source="{Binding Source}" Margin="12,0,12,12" 
         HorizontalAlignment="Center" VerticalAlignment="Center" 
         toolkit:TiltEffect.IsTiltEnabled="True"/> 
       <Button x:Name="photoRefreshButton" Grid.Row="0" Grid.Column="5" 
         BorderBrush="Transparent" Click="photoRefreshButton_Click" 
         toolkit:TiltEffect.IsTiltEnabled="True"> 
        <Image Source="Assets/Buttons/back.png"/> 
       </Button> 
      </Grid> 

我希望能夠切換前景色和背景時,按下並釋放按鈕,但我不確定如何改變按鈕模板風格這一點。我想用以下設置:

Not Pressed 
Foreground = #FF1BA1E2 (ARGB: 255, 27, 161, 226) 
background = transparent 

Pressed 
Foreground = Current theme foreground brush 
background = transparent 

編輯

嘗試這對我來說

<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 PhoneForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="#FF1BA1E2 "/> 
             </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> 

回答

0

的第一件事是,NotPressed狀態只使用默認的顏色,而不是禁用狀態。此外,如果您更改前景,它將更改某些文本的前景,但不會更改圖像。更容易改變圖像的「前景」可能是將其放在矩形內並使用不透明蒙版:

<Style x:Key="BackButtonStyle1" 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="Stroke" Storyboard.TargetName="circleBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="rectangle"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Stroke" Storyboard.TargetName="circleBorder"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Fill" Storyboard.TargetName="rectangle"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             </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}"> 
          <Grid > 
          <Rectangle x:Name="rectangle" Fill="#FF1BA1E2"> 
           <Rectangle.OpacityMask> 
            <ImageBrush ImageSource="Assets/Buttons/back.png"/> 
           </Rectangle.OpacityMask> 
          </Rectangle> 
          <Ellipse x:Name="circleBorder" Stroke="Red" StrokeThickness="5"/> 
           </Grid> 

         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

快速的問題,在'Disabled'VisualState上,你的意思是完全刪除第一個'ObjectAnimationUsingKeyFrames>'?此外,如果我要在按鈕周圍放置圓形邊框以模仿默認應用欄按鈕方案,您是否會建議使用後退按鈕創建單個圖像並將其作爲一個圖像進行循環,或者手動在按鈕周圍放置邊框,以及? – Matthew

+1

添加回來,以便在您禁用它時更改您的按鈕。還添加了一個橢圓以顯示圓形「邊框」。 –

0

沒有工作,有沒有你正試圖從XAML做什麼特別的原因嗎?國際海事組織從代碼隱藏的方式來做它更容易。只需添加這些行,編輯,以反映你想要的實際前景/背景下,該按鈕的Clicked事件處理程序:

var btn = sender as Button; 
if (btn != null) 
{ 
    btn.Foreground=.......; 
    btn.Background=.......; 
} 
+0

我唯一的按鈕事件處理程序是'Clicked'事件。上述情況如何反映在受壓和失能狀態?我只是想在xaml中做到這一點,因爲有了像以前一樣修改模板的經驗,它似乎可以在沒有代碼的情況下正常工作。 – Matthew

+0

對不起,我的意思是說'Clicked'事件,但忘記寫在我的答案。但是,如果你想在每次點擊之間切換狀態,就很容易了:if(ForeGround == OriginalState)ForeGround = ModifiedState else ForeGround = OriginalState – jose