2016-05-09 4 views
0

IsMouseOverとIsPressedイベントで異なるハイライト効果を持つ画像ボタンを作成したいと思います。私が使用しているコード:同じコントロールの複数のDataTriggerが機能しないのはなぜですか?

<Button Style="{StaticResource AccentedSquareButtonStyle}" x:Name="button" Background="Transparent" BorderThickness="0" Foreground="Transparent" Grid.Column="1" HorizontalAlignment="Left" Margin="142,-2,0,0" VerticalAlignment="Top" Width="75"> 
      <Grid Background="Transparent"> 
       <ContentControl> 
        <ContentControl.Style> 
         <Style TargetType="{x:Type ContentControl}"> 
          <Setter Property="Content" Value="{StaticResource printIcon}"/> 
          <Style.Triggers> 
           <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsPressed}" Value="True" > 
            <Setter Property="Content" Value="{StaticResource printIconClicked}"/> 
           </DataTrigger> 
           <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsMouseOver}" Value="True" > 
            <Setter Property="Content" Value="{StaticResource printIconHighlighted}"/> 
           </DataTrigger> 
          </Style.Triggers> 
         </Style> 
        </ContentControl.Style> 
       </ContentControl> 
      </Grid> 
     </Button> 

私は別に DataTriggersを使用する場合、コードのみ動作しますが、この形式でのみマウスオーバーハイライトする効果が使用されています。私はMahAppsフレームワークを使用しています。 私は何が間違っていますか?

+0

データ・トリガ場合は順番を入れ替えます。 DataTriggersのポジションを逆転させると機能しますが、私には理由が分かりません。 –

答えて

1

あなたが知っているとおり、これはあなたのTriggersが定義されている順番と関係があります。 2つの異なるパス(両方ともマウスに関連しています)にバインドするので、定義されている順序は、最初にトリガーされるものを決定するので重要です。

ボタンをクリックすると、イベントの順序は次のとおりです。

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsPressed}" Value="True" > 
    <Setter Property="Content" Value="{StaticResource printIconClicked}"/> 
</DataTrigger> 
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsMouseOver}" Value="True" > 
    <Setter Property="Content" Value="{StaticResource printIconHighlighted}"/> 
</DataTrigger> 

  1. マウスは
  2. オーバーボタンは、現在のトリガーで何が起こるか、そう

を押して置きました

マウスホバーは、実際に設定を行っている唯一のマウスですeポインタが押されている間、ポインタはボタンの上にあります。このインプリメンテーションは、ボタンを押してからドラッグして左クリックしたままでも動作します。しかしこれは理想的ではありません。 (順番は関係ありませんので)修正するには

は、ちょうど私が試行錯誤を使用してそれを考え出し

<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsMouseOver}" Value="True" > 
    <Setter Property="Content" Value="{StaticResource printIconHighlighted}"/> 
</DataTrigger> 
<DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=IsPressed}" Value="True" > 
    <Setter Property="Content" Value="{StaticResource printIconClicked}"/> 
</DataTrigger> 
-1

マルチトリガーの詳細については、Wpfチュートリアルのブログをご覧ください。リンクは以下のとおりです。 http://www.wpf-tutorial.com/styles/multi-triggers-multitrigger-multidatatrigger/

これがあなたの質問に答えてくれたら教えてください。

+0

ありがとう!それは私を助けません。トリガが、IsMouseOver、IsPressedの順で記述されている場合にのみ、なぜトリガが機能するのか分かりました。 –

関連する問題