如何在 WPF 中应用多种样式 [英] How to apply multiple styles in WPF

查看:41
本文介绍了如何在 WPF 中应用多种样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WPF 中,如何将多个样式应用于 FrameworkElement?例如,我有一个已经有样式的控件.我也有一个单独的风格,我想在不影响第一个风格的情况下添加它.样式有不同的 TargetType,所以我不能只用另一种来扩展.

解决方案

我认为简单的答案是你不能做(至少在这个版本的 WPF 中)你想做的事.

强>

也就是说,对于任何特定元素,只能应用一种样式.

但是,正如上面其他人所说的,也许您可​​以使用 BasedOn 来帮助您.查看以下松散的 xaml.在其中,您将看到我有一个基本样式,它设置了一个属性,该属性存在于我想要应用两种样式的元素的基类中.并且,在基于基本样式的第二个样式中,我设置了另一个属性.

所以,这里的想法......是如果你能以某种方式分离你想要设置的属性......根据你想要设置多种样式的元素的继承层次......你可能有一种解决方法.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml><页面资源><Style x:Key="baseStyle" TargetType="FrameworkElement"><Setter Property="Horizo​​ntalAlignment" Value="Left"/></风格><Style TargetType="Button" BasedOn="{StaticResource baseStyle}"><Setter Property="Content" Value="Hello World"/></风格></Page.Resources><网格><按钮宽度=200"高度=50"/></网格></页面>


希望这会有所帮助.

注意:

特别要注意的一件事.如果将第二个样式(在上面的第一组 xaml 中)中的 TargetType 更改为 ButtonBase,则不会应用这两个样式.但是,请查看下面的 xaml 以绕过该限制.基本上,这意味着您需要为 Style 指定一个键并使用该键引用它.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml><页面资源><Style x:Key="baseStyle" TargetType="FrameworkElement"><Setter Property="Horizo​​ntalAlignment" Value="Left"/></风格><Style x:Key="derivedStyle" TargetType="ButtonBase" BasedOn="{StaticResource baseStyle}"><Setter Property="Content" Value="Hello World"/></风格></Page.Resources><网格><Button Width="200" Height="50" Style="{StaticResourcederivedStyle}"/></网格></页面>

In WPF, how would I apply multiple styles to a FrameworkElement? For instance, I have a control which already has a style. I also have a separate style which I would like to add to it without blowing away the first one. The styles have different TargetTypes, so I can't just extend one with the other.

解决方案

I think the simple answer is that you can't do (at least in this version of WPF) what you are trying to do.

That is, for any particular element only one Style can be applied.

However, as others have stated above, maybe you can use BasedOn to help you out. Check out the following piece of loose xaml. In it you will see that I have a base style that is setting a property that exists on the base class of the element that I want to apply two styles to. And, in the second style which is based on the base style, I set another property.

So, the idea here ... is if you can somehow separate the properties that you want to set ... according the inheritance hierarchy of the element you want to set multiple styles on ... you might have a workaround.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <Style x:Key="baseStyle" TargetType="FrameworkElement">
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
        <Style TargetType="Button" BasedOn="{StaticResource baseStyle}">
            <Setter Property="Content" Value="Hello World"/>
        </Style>
    </Page.Resources>
    <Grid>
        <Button Width="200" Height="50"/>
    </Grid>
</Page>


Hope this helps.

Note:

One thing in particular to note. If you change the TargetType in the second style (in first set of xaml above) to ButtonBase, the two Styles do not get applied. However, check out the following xaml below to get around that restriction. Basically, it means you need to give the Style a key and reference it with that key.

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Page.Resources>
        <Style x:Key="baseStyle" TargetType="FrameworkElement">
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
        <Style x:Key="derivedStyle" TargetType="ButtonBase" BasedOn="{StaticResource baseStyle}">
            <Setter Property="Content" Value="Hello World"/>
        </Style>
    </Page.Resources>
    <Grid>
        <Button Width="200" Height="50" Style="{StaticResource derivedStyle}"/>
    </Grid>
</Page>

这篇关于如何在 WPF 中应用多种样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆