本文介绍了EventTrigger中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以检查事件触发器中的条件吗?我该如何仅使用XAML来做类似的事情?

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

推荐答案

按钮和菜单项具有IsPressed属性,该属性对您有用,而其他控件则可以.但是,使用某些附加行为添加IsPressed属性很容易.这将使您可以像这样编写XAML:

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>

附加的行为类别:

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

这篇关于EventTrigger中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-07 03:16