2

我需要在鼠标悬停时显示不同的图像Image
目前我正在这样做

 <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <!-- Hover image -->
                                <Setter Property="Cursor" Value="Hand"/>
                                <Setter Property="Source" Value="C:\Users\Images\Hi.png"/>
                            </Trigger>
 </Style.Triggers>  

它工作正常。但我需要在显示不同tag的图片之前检查属性。 也就是说,如果值为 1 我需要显示如果它是 2 我需要显示。 我知道这可以通过处理图像和事件的代码来实现。 但我想在 XAML 中做到这一点。有什么线索吗?Image
TagHi1.pnghi2.png
MouseEnterMouseLeave

4

2 回答 2

4

您可以使用MultiDataTrigger

由于 DataTrigger 对关联的 DataContext/Binding 进行操作,因此需要使用 Image 控件。

    <Style.Triggers> 
       <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsMouseOver}" Value="True"/>
            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="1"/>

        </MultiDataTrigger.Conditions>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Source" Value="C:\Users\Images\Hi1.png"/>
        </MultiDataTrigger>

        <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsMouseOver}" Value="True"/>
            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Tag}" Value="2"/>

        </MultiDataTrigger.Conditions>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Source" Value="C:\Users\Images\Hi2.png"/>
        </MultiDataTrigger>
    </Style.Triggers> 
于 2013-01-02T09:20:21.467 回答
1

我创建了一个 UserControl,它允许您更改 MouseOver 上图像的颜色。有一个矩形使用两种 SolidColorBrush 样式来切换颜色并将图像显示为 OpacityMask。

这 a) 允许您使任何图像成为您选择的任何颜色,并且 b) 照顾您在每次需要新按钮时都必须重写事件逻辑。

注意:Source 必须在代码中设置,因为它不能直接在 XAML 中绑定。

ImageButton.xaml:

<UserControl x:Class="ImageButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Width="Auto"
        Height="Auto" 
        Cursor="Hand">
    <Rectangle x:Name="Border">
        <Rectangle.OpacityMask>
            <ImageBrush/>
        </Rectangle.OpacityMask>
        <Rectangle.Style>
            <Style TargetType="{x:Type Rectangle}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Fill" Value="{DynamicResource ImageHoverBackground}" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="false">
                        <Setter Property="Fill" Value="{DynamicResource ImageBackground}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>
</UserControl>

ImageButton.xaml.cs:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyNamespace
{
    public partial class ImageButton : UserControl
    {
        public static DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(ImageButton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnSourceChanged));
        public ImageSource Source
        {
            get
            {
                return (ImageSource)GetValue(SourceProperty);
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }
        private static void OnSourceChanged(DependencyObject Object, DependencyPropertyChangedEventArgs e)
        {
            ImageButton Button = Object as ImageButton;
            var ImageBrush = Button.Border.OpacityMask as ImageBrush;
            ImageBrush.ImageSource = Button.Source;
        }

        public ImageButton()
        {
            InitializeComponent();
        }
    }
}
于 2016-03-09T02:05:21.560 回答