wpf - 事件触发器中的条件

标签 wpf triggers eventtrigger

我可以检查事件触发器内的条件吗?我怎样才能只使用 XAML 做这样的事情?

 <EventTrigger RoutedEvent="MouseDown">
    <Trigger Property="IsPressed" Value="true">
       <Setter Property = "Foreground" Value="Green"/>

最佳答案

按钮和菜单项有一个 IsPressed 属性,可以为您工作,但其他控件可以。然而,使用某些附加行为添加 IsPressed 属性很容易。这将让您像这样编写 XAML:

<TextBlock Text="Hello" TriggerTest:IsPressedBehavior.MonitorMouse="true">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}"> 
            <Setter Property="Foreground" Value="Red" />
            <Style.Triggers> 
                <Trigger Property="TriggerTest:IsPressedBehavior.IsPressed" Value="True"> 
                    <Setter Property="Foreground" Value="Green" /> 
                </Trigger>
            </Style.Triggers> 
        </Style> 
    </TextBlock.Style>
</TextBlock>

附加行为类:
using System;
using System.Windows;
using System.Windows.Input;

namespace TriggerTest
{
    public static class IsPressedBehavior
    {
        public static bool GetMonitorMouse(DependencyObject obj)
        {
            return (bool)obj.GetValue(MonitorMouseProperty);
        }

        public static void SetMonitorMouse(DependencyObject obj, bool value)
        {
            obj.SetValue(IsPressedProperty, value);
        }

        public static readonly DependencyProperty MonitorMouseProperty =
            DependencyProperty.RegisterAttached("MonitorMouse",
                                                typeof(bool),
                                                typeof(IsPressedBehavior),
                                                new UIPropertyMetadata(false, OnMonitorMouse));

        public static bool GetIsPressed(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsPressedProperty);
        }

        public static void SetIsPressed(DependencyObject obj, bool value)
        {
            obj.SetValue(IsPressedProperty, value);
        }

        public static readonly DependencyProperty IsPressedProperty =
            DependencyProperty.RegisterAttached("IsPressed",
                                                typeof(bool),
                                                typeof(IsPressedBehavior),
                                                new UIPropertyMetadata(false));

        private static void OnMonitorMouse(DependencyObject depObj, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            UIElement uiElement = depObj as UIElement;
            if (uiElement == null)
            {
                return;
            }
            if ((bool)dependencyPropertyChangedEventArgs.NewValue)
            {
                uiElement.MouseDown += OnMouseDown;
                uiElement.MouseUp += OnMouseUp;
                uiElement.MouseLeave += OnMouseLeave;
                uiElement.MouseEnter += OnMouseEnter;
            }
            else
            {
                uiElement.MouseDown -= OnMouseDown;
                uiElement.MouseUp -= OnMouseUp;
                uiElement.MouseLeave -= OnMouseLeave;
                uiElement.MouseEnter -= OnMouseEnter;
            }
        }

        private static void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            SetIsPressed(sender as DependencyObject, true);
        }

        private static void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            SetIsPressed(sender as DependencyObject, false);
        }

        private static void OnMouseLeave(object sender, MouseEventArgs e)
        {
            SetIsPressed(sender as DependencyObject, false);
        }

        static void OnMouseEnter(object sender, MouseEventArgs e)
        {
            SetIsPressed(sender as DependencyObject, e.LeftButton == MouseButtonState.Pressed || e.MiddleButton == MouseButtonState.Pressed || e.RightButton == MouseButtonState.Pressed);
        }
    }
}

关于wpf - 事件触发器中的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3353786/

相关文章:

wpf - 在运行时删除图像

c# - WPF 中的 ListView 和 MySQL

mysql - 如果某些数据相同,如何使用触发器插入数据

ruby-on-rails - 如何使用 PostgreSQL 触发器?

c# - WPF Interaction.Triggers MouseDown EventTrigger 不适用于 Canvas 元素

wpf - 事件触发器在 Storyboard 上不起作用

c# - 向 DataGrid 添加大量项目时 UI 卡住

c# - 如何在 WPF 图表中为 Y 轴混合指数/小数格式

oracle - ORA-04091 : table xx_xx is mutating, 触发器/函数可能看不到它