2014-09-04 143 views
0

我設置圖像按鈕,這樣更改按鈕圖像8

<Button x:Name="btToBackgroundPage" Click="toBackground_Click" Width="250" Height="100" BorderThickness="0"> 
    <Image Source="/Assets/Image/A.png" Stretch="Fill"/> 
</Button> 

所以,當我點擊它,我想按鈕中的圖片更改爲類似「B代碼.png「

我知道我可以通過使用按鈕事件按鈕和更改圖像源,但我的應用程序有這樣的許多按鈕,我只是想知道是否有無論如何設置在XAML?

回答

0

我不認爲你可以在xaml本身做到這一點,因爲不知何故,你必須有一個事件處理程序爲您的image點擊事件來更改button的圖像源。

How to change the background image of button on runtime?將會有幫助。

希望它有幫助!

+0

所以與每個按鈕我不得不用了兩個事件,按下並保持按下只是改變形象?並與我的所有按鈕在應用程序?? MS應該改變WP9 :( – user3448806 2014-09-04 04:39:28

+0

)如果你真的想要做任何事情,當你離開按鈕點擊,那麼你可以去這兩個事件,否則只需點擊或點擊事件就可以了。爲什麼你沒有所有按鈕的通用模板,以便您可以將它應用於每一個按鈕,但功能方面,如果您必須爲每個按鈕執行不同的操作,則必須單獨處理它們。 – Kulasangar 2014-09-04 06:45:50

0

這看起來可能有點複雜。但它工作正常。

XAML:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="11,0,12,0" Background="Black"> 

      <Button x:Name="b1" Content="Button" HorizontalAlignment="Left" Margin="208,421,0,0" VerticalAlignment="Top" MouseEnter="MouseEnter" MouseLeave="MouseLeave" Tag="b1"> 
       <Button.Background> 
        <ImageBrush x:Name="b1bg" ImageSource="/Assets/WP_20140902_001.jpg" /> 
       </Button.Background> 
       <Button.Template> 
        <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.TargetName="ContentContainer" 
             Storyboard.TargetProperty="Foreground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedHighlightBackground" 
             Storyboard.TargetProperty="Background"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="BorderBrush"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" 
             Storyboard.TargetProperty="Foreground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="BorderBrush"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="Background"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" 
      BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" 
      Background="{TemplateBinding Background }" Margin="{StaticResource PhoneTouchTargetOverhang}" > 
           <Border x:Name="PressedHighlightBackground" Background="Transparent"> 
            <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" 
         HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
         VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" 
         Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> 
           </Border> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Button.Template> 
      </Button> 
      <Button x:Name="b2" Content="Button" HorizontalAlignment="Left" Margin="71,421,0,0" VerticalAlignment="Top" MouseEnter="MouseEnter" MouseLeave="MouseLeave" Tag="b2"> 
       <Button.Background> 
        <ImageBrush x:Name="b2bg" ImageSource="/Assets/WP_20140902_001.jpg"/> 
       </Button.Background> 
       <Button.Template> 
        <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.TargetName="ContentContainer" 
             Storyboard.TargetProperty="Foreground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedHighlightBackground" 
             Storyboard.TargetProperty="Background"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="BorderBrush"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="Disabled"> 
             <Storyboard> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" 
             Storyboard.TargetProperty="Foreground"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="BorderBrush"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
              <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
             Storyboard.TargetProperty="Background"> 
               <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
              </ObjectAnimationUsingKeyFrames> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" 
      BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" 
      Background="{TemplateBinding Background }" Margin="{StaticResource PhoneTouchTargetOverhang}" > 
           <Border x:Name="PressedHighlightBackground" Background="Transparent"> 
            <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" 
         HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
         VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" 
         Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> 
           </Border> 
          </Border> 
         </Grid> 
        </ControlTemplate> 
       </Button.Template> 
      </Button> 
     </Grid> 

CS ::

private void MouseLeave(object sender, MouseEventArgs e) 
{ 
    Button temp = sender as Button; 

    var actionString = temp.Tag; 
    var value = this.FindName(actionString + "bg"); 
    ((ImageBrush)value).ImageSource = new BitmapImage(new Uri("/Assets/WP_20140902_001.jpg", UriKind.Relative)); 
} 

private void MouseEnter(object sender, MouseEventArgs e) 
{ 
    Button temp = sender as Button; 
    var actionString = temp.Tag; 
    var value= this.FindName(actionString + "bg"); 
    ((ImageBrush)value).ImageSource = new BitmapImage(new Uri("/Assets/splash_480_720.png", UriKind.Relative)); 
}