2016-09-14 147 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。但那是另一个话题。