如何将文本框的效果添加到样式中

时间:2011-03-01 16:35:59

标签: wpf textbox styles effect

我正在尝试为样式添加效果以便重复使用它,但是由于某种原因它不起作用...

<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
    <Style.Resources>
        <TextBox.Effect x:Key="EffectStyle">
            <DropShadowEffect BlurRadius="56" 
                              Direction="392" 
                              Color="#FF872E2E" 
                              RenderingBias="Quality"/>
       </TextBox.Effect>
    </Style.Resources>

    <Setter Property="Height" Value="25"/>
    <Setter Property="Width" Value="120"/>
    <Setter Property="HorizontalAlignment" Value="Right"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="TextAlignment" Value="Center"/>
</Style>

但我如何添加样式部分? (我如何申报效果?)

谢谢

2 个答案:

答案 0 :(得分:25)

尝试将效果添加为Setter而不是

<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Effect">
        <Setter.Value>
            <DropShadowEffect BlurRadius="56"
                              Direction="392"
                              Color="#FF872E2E"
                              RenderingBias="Quality"/>
        </Setter.Value>
    </Setter>
    <Setter Property="Height" Value="25"/>
    <Setter Property="Width" Value="120"/>
    <Setter Property="HorizontalAlignment" Value="Right"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="TextAlignment" Value="Center"/>
</Style>

或者,如果您希望将效果作为样式中的资源,您可以像这样做

<Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
    <Style.Resources>
        <DropShadowEffect x:Key="dropShadowEffect"
                          BlurRadius="56"
                          Direction="392"
                          Color="#FF872E2E"
                          RenderingBias="Quality"/>
    </Style.Resources>
    <Setter Property="Effect" Value="{StaticResource dropShadowEffect}"/>
    <!--...-->
</Style>

答案 1 :(得分:2)

您还可以将效果设为全局资源,以便将其与其他样式/控件一起使用:

<Grid>
    <Grid.Resources>
        <DropShadowEffect x:Key="dropShadowEffect" BlurRadius="56" 
                            Direction="392" 
                            Color="#FF872E2E" 
                            RenderingBias="Quality"/>

        <Style x:Key="NumericTextBoxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Effect" Value="{StaticResource dropShadowEffect}" />
            <Setter Property="Height" Value="25"/>
            <Setter Property="Width" Value="120"/>
        </Style>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>

    <TextBox Style="{StaticResource NumericTextBoxStyle}" />
    <TextBox Style="{StaticResource NumericTextBoxStyle}" Grid.Row="1" />

    <ComboBox Effect="{StaticResource dropShadowEffect}" Grid.Row="2" />
</Grid>