首页 > 解决方案 > wpf中的多种样式

问题描述

我想TextBox从静态资源中定义我的设计,如何应用它?

现在我有:

<TextBox  Style="{StaticResource TextBoxHeight }" />

在这里Page.Resources

<Page.Resources>
        <Style x:Key="TextBoxHeight" TargetType="{x:Type TextBox}" >
            <Setter Property="Height" Value="20"/>

        </Style>
        <Style x:Key="TextBoxBorder" TargetType="{x:Type Border}"  >
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
 </Page.Resources>

但我需要:

<TextBox   Style="{StaticResource TextBoxHeight }" Style="{StaticResource TextBoxBorder }" />

但它给出了错误“属性'样式'被设置多次”

标签: wpfxaml

解决方案


您不能Style多次设置该属性。而且您不能将 aStyle与 aTargetType应用于Bordera TextBoxBorder但是在样式Resources字典中添加隐式Button样式应该可以:

<Style x:Key="TextBoxHeight1" TargetType="{x:Type TextBox}" >
    <Setter Property="Height" Value="20"/>
    <Style.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="10"/>
        </Style>
    </Style.Resources>
</Style>

推荐阅读