gpt4 book ai didi

wpf - 使用 EventTrigger 设置属性

转载 作者:行者123 更新时间:2023-12-03 05:29:36 25 4
gpt4 key购买 nike

我希望能够使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com