关于wpf:单击按钮是否可以仅使用XAML触发器来永久更改图像的来源?

Is it possible for a button click to permanently change an image's source using only a XAML trigger?

我正在尝试解决WPF数据网格上的以下问题:

我已经将DataGrid项模板化为包含一个按钮-单击该按钮时,它将显示网格的RowDetails。我无法按我想要的方式来设置该按钮。具体来说,我希望它显示为"超链接",并在其旁边带有一个小加号图标。单击以显示RowDetails时,该图标应更改为减号。

这是我尝试应用于按钮的ControlTemplate:

1
2
3
4
5
6
7
8
<ControlTemplate x:Key="linkButtonTemplate" TargetType="{x:Type Button}">
     <StackPanel Orientation="Horizontal">                    
          <Image x:Name="expanderIcon" Source="Images/Plus.png"/>
          <TextBlock Foreground="Blue" TextDecorations="Underline" Padding="5 0 0 0">
               <ContentPresenter />
          </TextBlock>                    
     </StackPanel>
</ControlTemplate>

因此,当单击按钮时,我想更改该Image \\的Source属性。当再次单击它时,我想将其更改回。我遇到了以下建议的解决方案,但似乎都不是理想的解决方案:

1。在Button.IsPressed事件上使用触发器:有效! ...除非事件结束,否则图像会还原。 (即,一旦IsPressed属性不再为true)。因此它不起作用。

2。使用EventTrigger:这些至少可以让您捕获Button的Click事件...和

Unlike Trigger, EventTrigger has no
concept of termination of state, so
the action will not be undone once the
condition that raised the event is no
longer true.

太棒了!但是它们似乎只允许您使用情节提要动作来响应,而不是简单的属性Setter。除非我缺少任何东西,否则这些都不适合这种情况。

3。使用附加行为:我目前正在研究此选项,但我忍不住想到应该有一种XAML包含的方法来执行此操作。如果不是,那就这样-我将学习"附加行为"。

但是我希望有人对此我没有遇到过的看法。

例如,是否有一种方法可以钩住DataRow的属性,该属性包含指示RowDetails当前是否可见的按钮?看来您也需要破解背后的代码才能完成该任务……我错了吗?


我不会说EventTriggers并不是这个意思,单帧动画很好,甚至可以在某些控件的默认模板中找到,例如

1
2
3
4
5
6
7
8
9
10
11
<VisualState Name="CalendarButtonFocused">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="CalendarButtonFocusVisual" Storyboard.TargetProperty="Visibility" Duration="0">
            <DiscreteObjectKeyFrame KeyTime="0">
                <DiscreteObjectKeyFrame.Value>
                    <Visibility>Visible</Visibility>
                </DiscreteObjectKeyFrame.Value>
            </DiscreteObjectKeyFrame>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

因此,这是一个如何以这种方式更改源的示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<Image Name="img" Source="C:\\Users\\Public\\1.png"/>
<Button Content="!">
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <EventTrigger.Actions>
                <BeginStoryboard>
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.Target="{x:Reference img}"
                                                       Storyboard.TargetProperty="Source">
                            <DiscreteObjectKeyFrame KeyTime="0:0:0">
                                <DiscreteObjectKeyFrame.Value>
                                    <BitmapImage UriSource="C:\\Users\\Public\\2.png"/>
                                </DiscreteObjectKeyFrame.Value>
                            </DiscreteObjectKeyFrame>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger.Actions>
        </EventTrigger>
    </Button.Triggers>
</Button>

对于这种情况,您可以使用重新模板化的ToggleButton,并将图像可见性绑定到IsChecked属性。将RowDetails的可见性与其绑定起来也将变得容易得多。