2016-09-14 146 views
0

當我點擊Button時,其中的圖像必須更改並保持更改。我試圖在ButtonStyle上使用Triggers,將圖像綁定到IsPressed屬性,但是當發佈Button時,圖像返回前一個。請幫忙。wpf觸發屬性IsPressed不能正常工作

<Button Content="Easy Locate" Height="20" Width="85" Margin="0,2,0,0"> 
    <Button.Style> 
    <Style TargetType="{x:Type Button}"> 
     <Style.Triggers> 
     <Trigger Property="IsPressed" Value="True"> 
      <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
       <Border BorderThickness="0"> 
        <Image Source="/AltusClient;component/Images/waiting.png" 
         Height="20" 
         Width="25"/> 
       </Border> 
       <!--<ControlTemplate.Triggers> 
        <Trigger Property="IsPressed" Value="True"> 
        <Setter Property="Background" Value="Transparent"/> 
        <Setter Property="BorderThickness" Value="0"/> 
        </Trigger> 
       </ControlTemplate.Triggers>--> 
       </ControlTemplate> 
      </Setter.Value> 
      </Setter> 
     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Button.Style> 
</Button> 
+0

你是說只要持續按下按鈕,IsPressed狀態就會持續下去嗎?這就是'IsPressed'的意思。你是說你想要點擊時改變按鈕內容*永久* –

+0

是的。這正是我想要的。我如何做到這一點。 – nikhil

+0

我想你應該可以在觸發器EnterActions中使用故事板來做到這一點,但這有點痛苦。早一點。 –

回答

1

下面是一個按您想要的方式工作的按鈕示例。通過添加另一個Trigger來將其Tag屬性設置爲null,這是一個空字符串,或除"ButtonHasBeenPressed"之外的其他任何事物,您都可以輕鬆地將該按鈕切換回其默認狀態。

<Button Content="Button Content"> 
    <Button.Style> 
     <Style TargetType="Button"> 
      <Style.Resources> 
       <ControlTemplate 
        x:Key="PressedTemplate" 
        TargetType="Button"> 
        <Border 
         Background="LightSkyBlue" 
         BorderBrush="DeepSkyBlue" 
         BorderThickness="4" 
         > 
         <TextBlock 
          Text="Replace this TextBlock with your own content" 
          Foreground="ForestGreen" 
          /> 
        </Border> 
       </ControlTemplate> 
      </Style.Resources> 
      <Style.Triggers> 
       <!-- 
       We can't set Template directly with DiscreteObjectKeyFrame because 
       "Cannot freeze this Storyboard timeline tree for use across threads". 

       So instead we kludge it by setting Tag and putting a trigger on that. 
       --> 
       <Trigger 
        Property="Tag" 
        Value="ButtonHasBeenPressed" 
        > 
        <Setter 
         Property="Template" 
         Value="{StaticResource PressedTemplate}" 
         /> 
       </Trigger> 
       <Trigger Property="IsPressed" Value="True"> 
        <!-- 
        Values set by a Trigger setter are automatically rolled 
        back when the Trigger condition no longer applies. This 
        is not true of values applied by a Storyboard in an enter 
        action. 
        --> 
        <Trigger.EnterActions> 
         <BeginStoryboard> 
          <Storyboard> 
           <ObjectAnimationUsingKeyFrames 
            BeginTime="00:00:00" 
            Storyboard.TargetProperty="Tag" 
            > 
            <DiscreteObjectKeyFrame 
             KeyTime="00:00:00" 
             Value="ButtonHasBeenPressed" 
             /> 
           </ObjectAnimationUsingKeyFrames> 
          </Storyboard> 
         </BeginStoryboard> 
        </Trigger.EnterActions> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Button.Style> 
</Button> 
+0

謝謝埃德讓我試試看,您可以請幫助嗎? – nikhil

0

您也可以使用代碼隱藏。

假設按鈕是:

<Button Click="Button_Click"> 
    <Image Source="/Image1.png"/> 
</Button> 

然後,後面的代碼可能是:

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var b = sender as Button; 

    if (b == null) 
     return; 

    var image = new Image { Source = GetImageSource(), }; 

    b.Content = image; 
} 

private BitmapImage GetImageSource() 
{ 
    var imageSource = new BitmapImage(); 

    imageSource.BeginInit(); 
    imageSource.UriSource = new Uri("/Image2.png", UriKind.Relative); 
    imageSource.EndInit(); 

    return imageSource; 
} 

這是一個簡化版本,即每次用戶點擊加載新的形象。如果你想避免不必要的二次加載,你可以設置一個私有標誌,如private bool _imageSet;,並將其設置爲Button_Click方法。

您也可以在窗口(或UserControl's)初始化的早期加載新圖像。

最後,我建議你把所有的圖像放在ResourceDictionary中,並通過StaticResourceKey在整個應用程序中調用它們,而不是全部使用URI。但那是另一個話題。