Как добавить эффект для текстового поля в стиле - PullRequest
12 голосов
/ 01 марта 2011

Я пытаюсь добавить эффект к стилю, чтобы использовать его повторно, но по какой-то причине он не работает ...

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

25 голосов
/ 01 марта 2011

Попробуйте вместо этого добавить эффект как сеттер

<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>
2 голосов
/ 01 марта 2011

Вы также можете сделать свой эффект глобальным ресурсом, чтобы использовать его с другими стилями / элементами управления:

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