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

标签 wpf xaml

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

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

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

<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>

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

<强>1。 Use a Trigger在 Button.IsPressed 事件上: 它有效! ...除非事件结束后图像会恢复。(即一旦 IsPressed 属性不再为 true)。所以它不起作用。

<强>2。使用EventTrigger :这些至少可以让您捕获按钮的单击事件...并且

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.

所以,太棒了!但它们似乎只允许您使用 Storyboard 操作进行响应,而不是简单的属性 setter 。除非我遗漏了什么,否则这些不适用于这种情况。

<强>3。使用Attached Behavior :我目前正在研究这个选项,但我就是忍不住想应该有一种包含XAML的方法来做到这一点。如果没有,那就这样吧——我会学习依恋行为。

但我希望有人对此有我没有遇到过的观点。

例如,是否有一种方法可以 Hook 包含指示 RowDetails 当前是否可见的按钮的 DataRow 属性?看来你也需要破解背后的代码才能实现这一点......我错了吗?

最佳答案

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

<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>

下面是如何以这种方式更改源的示例:

<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>

关于wpf - 是否可以仅使用 XAML 触发器单击按钮来永久更改图像的源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3783355/

相关文章:

wpf - WPF:在一个ViewModel上单击按钮应在另一个ViewModel上执行命令

wpf - 如何在 Intranet 上运行 Full Trust XBAP?

c# - WPF 和 FontAwesome 连字符图标问题

c# - 在c#中加载xaml文件,获取具有指定名称的元素,添加按钮并保存文件

c# - 在不分组的情况下使用 Metro 风格的 GridView

wpf - 如何从 ContentTemplate 绑定(bind)到周围的自定义控件?

c# - 更新在另一个线程中创建的控件?

c# - ScaleTransform 有什么用?

c# - 在运行时更改 DataTrigger 绑定(bind)引用

c# - 为什么 DataContext 无法继承构造为附加属性的对象?