Condition in an EventTrigger

怎甘沉沦 提交于 2019-12-23 07:34:05

问题


Can I check condition inside an event trigger? how can I do something like that using only XAML?

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

回答1:


Buttons and menu items have an IsPressed property that would work for you but other controls does. It is however easy to add an IsPressed property using some attached behavior. This will let you write XAML like this:

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

Attached behavior class:

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);
        }
    }
}


来源:https://stackoverflow.com/questions/3353786/condition-in-an-eventtrigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!