gpt4 book ai didi

wpf - Enum 的 DataStateBehavior 而不是 bool?字符串?

转载 作者:行者123 更新时间:2023-12-04 14:35:43 26 4
gpt4 key购买 nike

WPF 中是否有一种简单的方法可以将 VisualStates 绑定(bind)到枚举值?有点像 DataStateBehavior,但对于枚举?

最佳答案

最好的方法是继续执行一个 Behavior 来做到这一点 -

public class EnumStateBehavior : Behavior<FrameworkElement>
{
public object EnumProperty
{
get { return (object)GetValue(EnumPropertyProperty); }
set { SetValue(EnumPropertyProperty, value); }
}

// Using a DependencyProperty as the backing store for EnumProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EnumPropertyProperty =
DependencyProperty.Register("EnumProperty", typeof(object), typeof(EnumStateBehavior), new UIPropertyMetadata(null, EnumPropertyChanged));

static void EnumPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue == null) return;

EnumStateBehavior eb = sender as EnumStateBehavior;

VisualStateManager.GoToElementState(eb.AssociatedObject, e.NewValue.ToString(), true);
}

}

用法非常简单——使用如下:
<i:Interaction.Behaviors>
<local:EnumStateBehavior EnumProperty="{Binding MyEnumProperty}" />
</i:Interaction.Behaviors>

关于wpf - Enum 的 DataStateBehavior 而不是 bool?字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3041744/

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