2017-03-21 11 views
2

の背景に画像を結合私持っている私のXAMLで、次のコード:トグルボタンEventTriggerBehavior-トグルボタン

<ToggleButton x:Name="play" Command="{Binding OnPlayButton}" CommandParameter="{Binding ElementName=media , Mode=TwoWay}" HorizontalAlignment="Left" Margin="573,638,0,0" VerticalAlignment="Top" Height="50" Width="50"> 
    <Image Name="MyImage" Source="Images/Control/Play.png"/> 
    <interactivity:Interaction.Behaviors> 
     <core:EventTriggerBehavior EventName="Checked"> 
      <core:ChangePropertyAction PropertyName="MyImage" Value="{Binding Source=Images/Control/Pause.png}" /> 
     </core:EventTriggerBehavior> 
     <core:EventTriggerBehavior EventName="Unchecked"> 
      <core:ChangePropertyAction PropertyName="MyImage" Value="{Binding Source=Images/Control/Play.png}" /> 
     </core:EventTriggerBehavior> 
    </interactivity:Interaction.Behaviors> 
</ToggleButton> 

何私はpause.pngするトグルボタンの背景を変更されて達成しようとしているときにクリックし、もう一度クリックするとplay.pngに変わります。 xamlで例外が発生していますが、これは正しい方法ですか?

答えて

1

ほとんどの場合、ビヘイビアがあります。しかし、PropertyNameはImageではありません。Sourceです。 TargetObjectMyImage

あなたのコードは以下のようになります。

<ToggleButton HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="True"> 
    <Interactivity:Interaction.Behaviors> 
     <Core:EventTriggerBehavior EventName="Checked"> 
      <Core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Source" Value="Images/Control/Pause.png" /> 
     </Core:EventTriggerBehavior> 
     <Core:EventTriggerBehavior EventName="Unchecked"> 
      <Core:ChangePropertyAction TargetObject="{Binding ElementName=MyImage}" PropertyName="Source" Value="Images/Control/Play.png"/> 
     </Core:EventTriggerBehavior> 
    </Interactivity:Interaction.Behaviors> 
    <Image x:Name="MyImage" Source="Images/Control/Play.png" Width="100" Height="100"/> 
</ToggleButton> 

Good Luck。

+1

こんにちは、これは正しい解決策です。 –

0

PropertyName="MyImage"は「MyImage」はコントロールの名前であり、ToggleButtonのプロパティではありません。私は2つのコンバータインスタンスのために使用しています上記の

<ToggleButton x:Name="play"> 
    <Image Source="Images/Control/Play.png" Visibility="{Binding IsChecked, ElementName=play, Converter={StaticResource BooleanToVisibilityConverter}" /> 
    <Image Source="Images/Control/Stop.png" Visibility="{Binding IsChecked, ElementName=play, Converter={StaticResource InverterBooleanToVisibilityConverter}" /> 
</ToggleButton> 

例の場合:

あなたが欲しいものを行う最も簡単な方法は、制御状態に応じて他と変更視界後に二つの画像を1つずつ追加することですboolの値をVisibilityの値に変換するもの。あなたは自分で書くことも、BooleanToVisibilityConverterのようなサードパーティのものをCimbalino Toolkitから使うこともできます。