2017-02-23 141 views
0

我正嘗試在WPF應用程序中創建一些按鈕,這些按鈕具有基於當前對按鈕執行的操作的觸發器事件。我無法添加多個觸發事件。在下面的代碼isPressed的觸發器不起作用,但isMouseOver做。如果我刪除了其中一個觸發器,但它們不能一起工作。WPF觸發器事件的問題

我還需要添加圖像更改,如果按鈕被禁用,但觸發屬性似乎不包含禁用的任何東西。我可能必須在C#代碼中進行此更改。如果有人有這個想法,這將是偉大的!

任何幫助表示讚賞!

<Button Name="testbutton"  
     Background="Transparent" 
     Cursor="Hand" 
     Visibility="Visible" Grid.Column="2" Grid.Row="1" Margin="10" ToolTip="Exits The Application"> 
      <Button.Template> 
       <ControlTemplate TargetType="Button"> 
        <StackPanel> 
         <Image Name="exitstatic" 
           Source="{StaticResource exit static}" 
           Stretch="Fill" 
           Visibility="Visible" /> 
         <Image Name="exithover" 
           Source="{StaticResource exit hover}" 
           Stretch="Fill" 
           Visibility="Collapsed" /> 
         <Image Name="exitdisabled" 
           Source="{StaticResource exit disabled}" 
           Stretch="Fill" 
           Visibility="Collapsed" /> 
        </StackPanel> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsPressed" 
           Value="true"> 
          <Setter TargetName="exitdisabled" 
            Property="Visibility" 
            Value="Visible" /> 
          <Setter TargetName="exithover" 
            Property="Visibility" 
            Value="Collapsed" /> 
         </Trigger> 

         <Trigger Property="IsMouseOver" 
             Value="true"> 
          <Setter TargetName="exithover" 
            Property="Visibility" 
            Value="Visible" /> 
          <Setter TargetName="exitstatic" 
            Property="Visibility" 
            Value="Collapsed" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Button.Template> 
     </Button> 

回答

1

每當IsPressed是真實的,IsMouseOver也是如此,只是因爲鼠標必須有按事情。實際上,應用了IsPressed觸發器設置器,但之後應用IsMouseOver觸發器設置器,並繼續執行IsPressed設置器所做的操作。

顛倒兩個觸發器的順序,所以IsPressed觸發器將被第二次評估,並且改爲由IsPressed設置的值。這是觸發器常見的問題。

就你的第二個問題而言,我從來沒有遇到任何問題<Trigger Property="IsEnabled" Value="False">

+0

@ mcavanaugh418,同意,觸發器按照列出的順序進行處理......無論是最後的勝利。 – DRapp

+1

謝謝埃德。非常簡單,我無法相信我沒有抓住那個開始。我也必須錯過IsEnabled屬性。感謝您指出,正是我需要的。 – mcavanaugh418