wpf - 使用 EventTrigger 设置属性

标签 wpf xaml triggers eventtrigger

我希望能够使用 EventTrigger 设置属性,但这有很多问题。

1) EventTriggers 仅支持 Actions,因此我必须使用 StoryBoard 来设置我的属性。

2)一旦我使用 Storyboard,我有两个选择:

  • 停止:动画停止后,值将恢复到动画开始之前
  • HoldEnd:这会锁定属性,以便代码和用户交互都无法更改动画所持有的属性。

在下面的示例中,我希望在单击按钮时将 IsChecked 属性设置为 False,并且我希望用户能够更改 IsChecked 和/或我希望能够更改代码中的属性。

示例:

<EventTrigger
    SourceName="myButton"
    RoutedEvent="Button.Click">
    <EventTrigger.Actions>
        <BeginStoryboard>
            <Storyboard>
                <BooleanAnimationUsingKeyFrames
                    Storyboard.TargetName="myCheckBox"
                    Storyboard.TargetProperty="IsChecked"
                    FillBehavior="Stop">
                    <DiscreteBooleanKeyFrame
                        KeyTime="00:00:00"
                        Value="False" />
                </BooleanAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>

我意识到我可以在 Storyboard 完成后使用“Completed”事件将值设置为 False。但是,在本例中,我希望将逻辑包含在 XAML 中,因为此逻辑将在自定义控件上使用,并且仅特定于 UI。

最佳答案

只需创建您自己的操作即可。

namespace WpfUtil
{
    using System.Reflection;
    using System.Windows;
    using System.Windows.Interactivity;


    /// <summary>
    /// Sets the designated property to the supplied value. TargetObject
    /// optionally designates the object on which to set the property. If
    /// TargetObject is not supplied then the property is set on the object
    /// to which the trigger is attached.
    /// </summary>
    public class SetPropertyAction : TriggerAction<FrameworkElement>
    {
        // PropertyName DependencyProperty.

        /// <summary>
        /// The property to be executed in response to the trigger.
        /// </summary>
        public string PropertyName
        {
            get { return (string)GetValue(PropertyNameProperty); }
            set { SetValue(PropertyNameProperty, value); }
        }

        public static readonly DependencyProperty PropertyNameProperty
            = DependencyProperty.Register("PropertyName", typeof(string),
            typeof(SetPropertyAction));


        // PropertyValue DependencyProperty.

        /// <summary>
        /// The value to set the property to.
        /// </summary>
        public object PropertyValue
        {
            get { return GetValue(PropertyValueProperty); }
            set { SetValue(PropertyValueProperty, value); }
        }

        public static readonly DependencyProperty PropertyValueProperty
            = DependencyProperty.Register("PropertyValue", typeof(object),
            typeof(SetPropertyAction));


        // TargetObject DependencyProperty.

        /// <summary>
        /// Specifies the object upon which to set the property.
        /// </summary>
        public object TargetObject
        {
            get { return GetValue(TargetObjectProperty); }
            set { SetValue(TargetObjectProperty, value); }
        }

        public static readonly DependencyProperty TargetObjectProperty
            = DependencyProperty.Register("TargetObject", typeof(object),
            typeof(SetPropertyAction));


        // Private Implementation.

        protected override void Invoke(object parameter)
        {
            object target = TargetObject ?? AssociatedObject;
            PropertyInfo propertyInfo = target.GetType().GetProperty(
                PropertyName,
                BindingFlags.Instance|BindingFlags.Public
                |BindingFlags.NonPublic|BindingFlags.InvokeMethod);

            propertyInfo.SetValue(target, PropertyValue);
        }
    }
}

在本例中,我绑定(bind)到 View 模型上名为 DialogResult 的属性。

<Grid>

    <Button>
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <wpf:SetPropertyAction PropertyName="DialogResult" TargetObject="{Binding}"
                                       PropertyValue="{x:Static mvvm:DialogResult.Cancel}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
        Cancel
    </Button>

</Grid>

关于wpf - 使用 EventTrigger 设置属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/942548/

相关文章:

c# - 如何迁移到 MVVM

c# - Windows Phone 8.1 中的 Adrotator。 XAML

MySQL:如何创建触发器来设置新行的创建日期

mysql - 无法更新存储触发器中的表,因为它已被调用此存储触发器的语句使用

MySQL - 使用触发器查找 SUM

wpf - Backgroundworker-完成后无法使用生成的位图

WPF DataGrid垂直线粗细

c# - Java+Swing 比 C#+WinForms/WPF 有什么优势?

c# - XAML WPF 多边形(三角形),仅在 2/3 边上有边框

c# - 无法绑定(bind)到 Windows 8 Store Apps 中的附加属性