2012-03-12 72 views
0

這與我的previous question相關,因爲它處理的是同一段代碼;現在我已經完成了按鈕背景的改變,問題是現在我需要實現相同的代碼,但不是用於ButtonPressed,而是用於點擊按鈕。我在代碼中添加了點擊處理程序,但它不起作用 - 背景沒有改變。我嘗試了不同的方法,即使使用位圖和圖像資源,但它不工作,更改根本不會發生。現在我想要實現背景圖像的更改,但它需要在XAML文件中完成,而不是在.cs中完成。再次代碼:在WPF內部的WPF控件上實現點擊事件

<Button x:Class="proba4.UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="120" d:DesignWidth="300" IsEnabled="True"> 
    <Button.Template> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Grid> 
       <Image Name="Normal" Source="C:\stuff\off_button.gif"/> 
       <Image Name="Pressed" Source="C:\stuff\on_button.gif" Visibility="Hidden"/> 

      </Grid> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsPressed" Value="True"> 
        <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> 
        <Setter TargetName="Pressed" Property="Visibility" Value="Visible"/> 
       </Trigger> 

      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Button.Template> 
</Button> 

請注意,我已經看過用於單擊某個按鈕屬性,但有沒有,我的一切互聯網上找到的實現正在處理中的.cs加入Button_Click方法該控件的代碼,因爲這不適合我,我需要找到另一種方式 - 希望像使用WPF完全實現的點擊控制。 我想這是微妙的,但我將不勝感激任何幫助,謝謝。

回答

1

這聽起來像你想要的行爲ToggleButton,而不是Button。點擊是一個離散事件,而不是控件進入的狀態,可以用屬性表示。 A ToggleButton單擊時在兩個(或三個)狀態之間來回切換,IsChecked屬性表示狀態,並且可以像在您的示例中使用IsPressed一樣在Trigger中進行綁定。