自定义控件在两种风格之间切换

发布于 2024-12-06 01:09:55 字数 943 浏览 1 评论 0原文

我正在制作一个自定义控件,它有两种截然不同的样式需求。一种用于基本外观,另一种用于更高级的外观。

我的控件包含以下枚举的依赖属性:

public enum ControlTypes
{
    Basic,
    Advanced
}

我在 generic.xaml 中创建了两种样式(具有非常不同的模板),并为每个样式提供了一个键。

在枚举属性的更改处理程序中,我试图找到样式并设置正确的样式。

private static void OnControlTypePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var myCustomControl = (MyCustomControl)d;

    var basicControlStyle = Application.Current.TryFindResource("BasicControlStyle") as Style;
    var advancedControlStyle = Application.Current.TryFindResource("AdvancedControlStyle") as Style;

    if (myCustomControl.ControlType == ControlTypes.Basic)
        myCustomControl.Style = basicControlStyle;
    else if (myCustomControl.ControlType == ControlTypes.Advanced)
        myCustomControl.Style = advancedControlStyle;            
}

这两种样式始终为空。我不确定如何从 generic.xaml 中获取样式。或者有更好的方法来交换我的风格吗?

I'm making a custom control which has two very different styling needs. One for a basic look, and another for a more advanced look.

My control contains a dependency property for the following enum:

public enum ControlTypes
{
    Basic,
    Advanced
}

I created two styles in the generic.xaml (with very different templates), and gave each a key.

Inside the change handler for the enum property I'm trying to find the styles and set the correct one.

private static void OnControlTypePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var myCustomControl = (MyCustomControl)d;

    var basicControlStyle = Application.Current.TryFindResource("BasicControlStyle") as Style;
    var advancedControlStyle = Application.Current.TryFindResource("AdvancedControlStyle") as Style;

    if (myCustomControl.ControlType == ControlTypes.Basic)
        myCustomControl.Style = basicControlStyle;
    else if (myCustomControl.ControlType == ControlTypes.Advanced)
        myCustomControl.Style = advancedControlStyle;            
}

The two styles are always null. I'm not sure how to get the styles from inside the generic.xaml. Or is there a better way to swap my styles?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

依 靠 2024-12-13 01:09:55

删除 OnControlTypePropertyChanged 中的代码并将类似的内容放入 XAML 中。请注意,我已绑定到一个名为 IsAdvanced 的属性,因为它更易于测试,但您可以通过更改 {x:Static namespace:nameofyourenum.Value} 的“True”来绑定到枚举。

<Style TargetType="local:SomeControl">
    <Style.Setters>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:SomeControl">
                    <StackPanel>
                        <TextBlock Text="DefaultTemplate"></TextBlock>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
    <Style.Triggers>
        <Trigger Property="IsAdvanced" Value="True">
            <Trigger.Setters>
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:SomeControl">
                            <TextBlock Text="Advanced Template"></TextBlock>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger.Setters>
        </Trigger>
    </Style.Triggers>
</Style>

请注意,这仍然给使用您的程序员提供了帮助。控制能力完全覆盖控制模板并做自己想做的事。您原来的方法不允许这样做。

Delete the code in OnControlTypePropertyChanged and put something like this in your XAML. Note that I have bound to a property called IsAdvanced because it was simpler for testing but you can bind to an enum by changing the "True" for {x:Static namespace:nameofyourenum.Value}

<Style TargetType="local:SomeControl">
    <Style.Setters>
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:SomeControl">
                    <StackPanel>
                        <TextBlock Text="DefaultTemplate"></TextBlock>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
    <Style.Triggers>
        <Trigger Property="IsAdvanced" Value="True">
            <Trigger.Setters>
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:SomeControl">
                            <TextBlock Text="Advanced Template"></TextBlock>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger.Setters>
        </Trigger>
    </Style.Triggers>
</Style>

Note that this still give the programmer who uses your control the ability to completely override the control template and do what they want. Your original approach didn't allow this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文