开发者

how to add effect for text box to style

开发者 https://www.devze.com 2023-02-13 23:54 出处:网络
I\'m trying to add an effect to style in order to reuse it, but from some reason it doesnt work... <Style x:Key=\"NumericTextBoxStyle\" TargetType=\"{x:Type TextBox}\">

I'm trying to add an effect to style in order to reuse it, but from some reason it doesnt work...

<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>

but how do i add the style part ? (also how do i declare for the effect ?)

thanks


Try to add the Effect as a Setter instead

<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>

Or if you want to have the Effect as a Resource in the Style you can do it like this

<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>


You can also make your effect a global resource, in order to use it with other styles/controls:

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号