gpt4 book ai didi

xaml - DataTriggerBehavior 不适用于枚举?

转载 作者:行者123 更新时间:2023-12-04 21:42:04 31 4
gpt4 key购买 nike

我正在尝试使用 DataTriggerBehavior来自行为 SDK。但它似乎不适用于枚举......否则我做错了什么。

您可以假设 DataContext这些示例是这样的(INotifyPropertyChanged 已实现,但我不打算在这里展示):

public class MyDataClass
{
public MyEnum ItemCommand { get; set; }
public string ItemCommandString { get; set; }
}

public enum MyEnum
{
EnumValue1
}

_Button.DataContext = new MyDataClass() { ItemCommand = MyEnum.EnumValue1,
ItemCommandString = "EnumValue1" };

这是不起作用的代码(尝试指定枚举值并检查 ItemCommand 枚举属性):
<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommand}"
Value="EnumValue1">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>

并且此代码(检查枚举资源)也不起作用:
<UserControl.Resources>
<local:MyEnum x:Key="_MyEnumValue">EnumValue1</local:MyEnum>
</UserControl.Resources>

<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommand}"
Value="{StaticResource _MyEnumValue}">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>

而这段代码(检查字符串)确实有效:
<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommandString}"
Value="EnumValue1">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>

DataTriggerBehavior 中指定枚举值的正确方法是什么? Value属性(property),这样这将起作用吗?

最佳答案

你可以写一个转换器:

 public class MyEnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
MyEnum myEnumValue = (MyEnum)value;
return myEnumValue.ToString();
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}

并在 XAML 中使用它:
<ToggleButton x:Name="_Button">
<Interactivity:Interaction.Behaviors>
<Core:DataTriggerBehavior Binding="{Binding ItemCommand, Converter={StaticResource MyEnumConverter}}"
Value="EnumValue1">
<Core:ChangePropertyAction PropertyName="Command"
TargetObject="{Binding ElementName=_Button}"
Value="{x:Null}">
</Core:ChangePropertyAction>
</Core:DataTriggerBehavior>
</Interactivity:Interaction.Behaviors>
</ToggleButton>

或直接绑定(bind)到示例中的字符串。不幸的是,WinRT 中的 DataTriggerBehavior 比 Windows Phone 8 中的 DataTrigger 更糟糕

关于xaml - DataTriggerBehavior 不适用于枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23728327/

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