2011-07-14 31 views
2

ボタンがあり、その背景画像を設定しました。タッチしたときに実行時にイメージを変更する方法と、タッチを削除したときに再度イメージを以前のイメージに変更する方法があります。私は次のコードを試しましたが、必要な結果が得られませんでした。 -クリックしたときにボタンの背景イメージを変更するにはどうすればいいですか?

private void button1_click(object sender, RoutedEventArgs e) 
{ 
    Button source = (Button)sender; 
    Image content = source.Content as Image; 
    if (null != content) 
    { 
     content.Source = new BitmapImage(new Uri("image path")); 
    } 
} 

ありがとうございます。

答えて

2

ボタンのテンプレートを変更する必要があります。 Expression Blendをお持ちの場合は、ボタンを右クリックしてEdit Template - > Edit a Copy ...を選択し、ブレンド(通常は左側)でStatesペインを選択し、そこに押し込み状態を選択します。オブジェクトとタイムラインでButtonBackgroundを選択し、Propertiesペインで(通常は右側に)必要なBackroundを設定します。

Blendがない場合は、ボタン用の新しいテンプレートを作成して、XressedをPressed状態に変更するだけです。

は、デフォルトのボタンテンプレートは次のようになります。

標準アニメーションが押された状態でPhoneForegroundBrushの背景を設定していることに注意してください
<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> 

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/> 
</ObjectAnimationUsingKeyFrames> 

あなたのイメージブラシリソースを定義されなければならないすべてPhoneForegroundBrushをここのブラシのキーに置き換えてください。

+0

値をイメージブラシに変更できますか?私はそれが@Alanそのように動作しないと思う – Mani

関連する問題