gpt4 book ai didi

c# - 基于对象中枚举字段的 xaml 样式

转载 作者:行者123 更新时间:2023-12-03 20:30:04 25 4
gpt4 key购买 nike

我有一个像这样的对象集合:

public enum ObjectType
{
Type1,
Type2
}

public class MyObject
{
...
public ObjectType ObjType;
public string Header;
...
}

我从示例应用程序中获得的样式之一是:

    <Style TargetType="{x:Type inf:MyObject}">
<Setter Property="Header" Value="{Binding Header}" />
</Style>

如何创建受 ObjectType 枚举字段约束的单独样式? IE。对于 ObjType 设置为 Type1 和 Type2 的 MyObject 有单独的样式吗?

我的字段确实实现了 INotifyPropertyChanged,这只是一些简短的示例代码

谢谢

最佳答案

我可能会通过触发器来做到这一点:

<Style TargetType="{x:Type inf:MyObject}">
<Setter Property="Header" Value="{Binding Header}" />
<Style.Triggers>
<DataTrigger Binding="{Binding ObjType}" Value="Type1">
<!-- A bunch of setters -->
</DataTrigger>
<DataTrigger Binding="{Binding ObjType}" Value="Type2">
<!-- Another bunch of setters -->
</DataTrigger>
</Style.Triggers>
</Style>

分离可能是不可能的。

嗯,您可以创建单独的样式,但需要使用 ResourceKey 手动应用它们:

<Style x:Key="Type1Style" TargetType="{x:Type inf:MyObject}"
BasedOn="{StaticResource {x:Type inf:MyObject}}">
<!-- Setters for type1 -->
</Style>
<Style x:Key="Type2Style" TargetType="{x:Type inf:MyObject}"
BasedOn="{StaticResource {x:Type inf:MyObject}}">
<!-- Setters for type2 -->
</Style>

关于c# - 基于对象中枚举字段的 xaml 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5587093/

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