如何为文本框添加效果到样式

发布于 2024-10-19 22:09:18 字数 828 浏览 4 评论 0原文

我试图向样式添加效果以便重用它,但由于某种原因它不起作用......

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

但是我如何添加样式部分? (另外我如何声明效果?)

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-10-26 22:09:18

尝试将效果添加为 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>

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>
零度° 2024-10-26 22:09:18

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文