c# - WPF:ControlTemplate 的 IsPressed 触发器不起作用

标签 c# wpf custom-controls controltemplate

我使用名为 ImageButton 的自定义控件来显示具有不同图像的按钮。 ImageButton 包含依赖属性:

public ImageSource DisabledImageSource
    {
        get { return (ImageSource)GetValue(DisabledImageSourceProperty); }
        set { SetValue(DisabledImageSourceProperty, value); }
    }


    public ImageSource NormalImageSource
    {
        get { return (ImageSource)GetValue(NormalImageSourceProperty); }
        set { SetValue(NormalImageSourceProperty, value); }
    }

    public ImageSource HoverImageSource
    {
        get { return (ImageSource)GetValue(HoverImageSourceProperty); }
        set { SetValue(HoverImageSourceProperty, value); }
    }

    public ImageSource PushedImageSource
    {
        get { return (ImageSource)GetValue(PushedImageSourceProperty); }
        set { SetValue(PushedImageSourceProperty, value); }
    }


    public static readonly DependencyProperty DisabledImageSourceProperty =
        DependencyProperty.Register("DisabledImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());


    public static readonly DependencyProperty NormalImageSourceProperty =
        DependencyProperty.Register("NormalImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());


    public static readonly DependencyProperty HoverImageSourceProperty =
        DependencyProperty.Register("HoverImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());

    public static readonly DependencyProperty PushedImageSourceProperty =
        DependencyProperty.Register("PushedImageSource", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata());

它的样式定义如下:

<Style TargetType="{x:Type local:ImageButton}">
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Image Name="image" Source="{Binding NormalImageSource, RelativeSource={RelativeSource TemplatedParent}}" Stretch="None" />
                </Grid>
                <ControlTemplate.Triggers>                    
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="image" Property="Source" Value="{Binding DisabledImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </Trigger>
                    <Trigger Property="Button.IsPressed" Value="True">
                        <Setter TargetName="image" Property="Source"  Value="{Binding PushedImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter TargetName="image" Property="Source" Value="{Binding HoverImageSource, RelativeSource={RelativeSource TemplatedParent}}"/>
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

IsPressed 外,一切正常。我永远看不到我在 PushedImageSource 中设置的图像,我也不知道为什么。 任何帮助将不胜感激:)

编辑 1:

这是测试它的 xaml 代码

<Window x:Class="SMEClient.WPF.Tester.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:is="clr-namespace:Project1.SearchBox;assembly=Project1"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Project1;component/SearchBox/ImageButton.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <is:ImageButton NormalImageSource="C:\Users\Public\Pictures\Sample Pictures\Koala.jpg"
                            PushedImageSource="C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg"
                            HoverImageSource="C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"/>
        </StackPanel>
    </Grid>
</Window>

最佳答案

您的触发器不是互斥的,在这种情况下,触发器添加到 Triggers 的顺序收藏开始发挥作用。你的最后一个触发器 IsMouseOver覆盖 Source IsPressed 之后的属性(property)触发器设置正确的图像,因为只有在鼠标悬停时才会按下按钮(当然是在使用鼠标时)。

尝试设置 IsPressedTriggers 中最后触发集合,以及 IsPressed即使 IsMouseOver 也会应用触发器也是如此。

关于c# - WPF:ControlTemplate 的 IsPressed 触发器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20237890/

相关文章:

c# - 找不到类型 'MyType' 的默认成员

c# - LINQ 性能计数与位置和计数

c# - 在 C# 中通过 sql 填充具有指定列的所有表名的列表

c# - 为什么跳转到 WPF for Business Application 而不是使用 Winforms

wpf - wpf自定义控件库和wpf类库有什么区别

c# - 如何从字符串中删除剩余的不需要的字符

c# - 浏览器是否缓存动态图像?

wpf - 如何在用户控件中定义一个依赖属性,仅从其子控件之一中冒出一个值?

ios - 如何在 MFMailcomposer View 中显示默认导航栏而不是 ios 中的自定义导航栏

c - 这就是我在 Windows 中使自定义控件透明且无闪烁的方法吗?或者我的这些步骤之一是错误的?