WPF 按钮和 TextBlock 样式如何相关?

发布于 2024-10-08 12:30:25 字数 4677 浏览 0 评论 0原文

我为“默认”按钮设置了自定义样式,还为 TextBlock 创建了自定义样式。如果我完全删除 TextBlock 样式,一切都会正常工作,但是一旦由于某种原因添加了 TextBlock 样式,按钮样式就会在按钮的文本“默认”状态上使用。似乎这里发生了某种继承,但我看不到 msdn 文档中的位置。这是怎么回事?

我正在使用 Expression Blend 4 - 另一个奇怪的事情是 Blend 中的预览看起来不错,但当我运行应用程序时,按钮样式在默认状态下不正确。以下是样式这似乎是冲突的:

        <ResourceDictionary>

        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="True">
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Color="White" Offset="0"/>
                                <GradientStop Color="#FFFDFF00" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
                    <Setter Property="RenderTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <ScaleTransform ScaleY="1.20" ScaleX="1.20"/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <ContentPresenter.Effect>
                                    <DropShadowEffect BlurRadius="3" ShadowDepth="4"/>
                                </ContentPresenter.Effect>
                            </ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True"/>
                            <Trigger Property="IsDefaulted" Value="True"/>
                            <Trigger Property="IsMouseOver" Value="True"/>
                            <Trigger Property="IsPressed" Value="True"/>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontFamily" Value="/Rtk;component/Fonts/#Segoe Print"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Background" Value="{x:Null}"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="NoWrap"/>
            <Setter Property="TextTrimming" Value="None"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect BlurRadius="3" ShadowDepth="4"/>
                </Setter.Value>
            </Setter>
            <Setter Property="FontFamily" Value="/Rtk;component/Fonts/#Segoe Print"/>
        </Style>
    </ResourceDictionary>

这就是我使用按钮控件本身的方式:(

<Button Content="Button Text" FontSize="24"/>  

请注意,此字体大小与我在默认样式中指定的大小不同,18 - 我想在此按钮的情况下覆盖它)

编辑: 实际的按钮条目在 MainWindow.xaml 中看起来像这样,除了我从 App.xaml 中提出的样式更改之外没有其他自定义:

<Button Content="Button" HorizontalAlignment="Left" Margin="336,0,0,274.226" VerticalAlignment="Bottom" Width="75"/>

为了说明我所看到的内容: 替代文本

I have a custom style for my 'default' Buttons, and also created a custom style for TextBlocks. If I remove the TextBlock style entirely, everything works fine, but once the TextBlock styling is added in for some reason the Button style is used on the Button's text 'default' state. It seems like some kind of inheritance is going on here but I can't see where in the msdn docs. What's going on?

I'm using Expression Blend 4-- and also another odd thing is that the preview in Blend looks fine, but when I RUN the application, the button styles are incorrect in their default state. Here's the styles which seem to be conflicting:

        <ResourceDictionary>

        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsMouseOver"
                         Value="True">
                    <Setter Property="Foreground">
                        <Setter.Value>
                            <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                <GradientStop Color="White" Offset="0"/>
                                <GradientStop Color="#FFFDFF00" Offset="1"/>
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
                    <Setter Property="RenderTransform">
                        <Setter.Value>
                            <TransformGroup>
                                <ScaleTransform ScaleY="1.20" ScaleX="1.20"/>
                                <SkewTransform/>
                                <RotateTransform/>
                                <TranslateTransform/>
                            </TransformGroup>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <ContentPresenter RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
                                <ContentPresenter.Effect>
                                    <DropShadowEffect BlurRadius="3" ShadowDepth="4"/>
                                </ContentPresenter.Effect>
                            </ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsFocused" Value="True"/>
                            <Trigger Property="IsDefaulted" Value="True"/>
                            <Trigger Property="IsMouseOver" Value="True"/>
                            <Trigger Property="IsPressed" Value="True"/>
                            <Trigger Property="IsEnabled" Value="False"/>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FontFamily" Value="/Rtk;component/Fonts/#Segoe Print"/>
            <Setter Property="FontWeight" Value="Bold"/>
            <Setter Property="FontSize" Value="18"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Background" Value="{x:Null}"/>
        </Style>
        <Style TargetType="{x:Type TextBlock}">
            <Setter Property="TextWrapping" Value="NoWrap"/>
            <Setter Property="TextTrimming" Value="None"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontSize" Value="16"/>
            <Setter Property="Effect">
                <Setter.Value>
                    <DropShadowEffect BlurRadius="3" ShadowDepth="4"/>
                </Setter.Value>
            </Setter>
            <Setter Property="FontFamily" Value="/Rtk;component/Fonts/#Segoe Print"/>
        </Style>
    </ResourceDictionary>

This is how I am using the Button control itself:

<Button Content="Button Text" FontSize="24"/>  

(note that this fontsize is different from the size I specified in the default style, 18 - I want to override it in this button's case)

Edit:
The actual button entry looks like this in MainWindow.xaml, there's no other customizations other than the style changes I posed from App.xaml:

<Button Content="Button" HorizontalAlignment="Left" Margin="336,0,0,274.226" VerticalAlignment="Bottom" Width="75"/>

To illustrate what I'm seeing:
alt text

alt text

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

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

发布评论

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

评论(3

痴梦一场 2024-10-15 12:30:25

只是一个快速的疯狂猜测,但是当按钮的内容是字符串时,它不是默认的文本块吗?

Just a fast wild guess, but when the content of a button is a string, isn't it default a textblock?

乖乖 2024-10-15 12:30:25

正如人们所建议的,您的按钮包含一个创建来保存内容的文本块,它从 app.xaml 中获取样式,您可以通过几种方式解决此问题,这里有一些:

将显式文本块放入您的按钮中,并且不应用样式:

  <Button Margin="272,192,277,0" VerticalAlignment="Top">
        <TextBlock Text="Button" Style="{x:Null}"/>
    </Button>

将文本块放入按钮样式中,也使用空样式:

 <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
      <TextBlock Text="{TemplateBinding Content}" Style="{x:Null}">
                       <TextBlock.Effect>
                          <DropShadowEffect BlurRadius="3" ShadowDepth="4"/>
                       </TextBlock.Effect>
      </TextBlock>
         </Grid>
              <ControlTemplate.Triggers>
                  <Trigger Property="IsFocused" Value="True"/>
                  <Trigger Property="IsDefaulted" Value="True"/>
                  <Trigger Property="IsMouseOver" Value="True"/>
                  <Trigger Property="IsPressed" Value="True"/>
                  <Trigger Property="IsEnabled" Value="False"/>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>

希望这两个之一适合您。

As people have suggested, your Button contains a Textblock created to hold the content, it is picking up the style from app.xaml, you can work around this in a few ways, here are a couple:

Put an explicit textblock into your button, and apply no style:

  <Button Margin="272,192,277,0" VerticalAlignment="Top">
        <TextBlock Text="Button" Style="{x:Null}"/>
    </Button>

Put a textblock into your button style, also with a null style:

 <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
      <TextBlock Text="{TemplateBinding Content}" Style="{x:Null}">
                       <TextBlock.Effect>
                          <DropShadowEffect BlurRadius="3" ShadowDepth="4"/>
                       </TextBlock.Effect>
      </TextBlock>
         </Grid>
              <ControlTemplate.Triggers>
                  <Trigger Property="IsFocused" Value="True"/>
                  <Trigger Property="IsDefaulted" Value="True"/>
                  <Trigger Property="IsMouseOver" Value="True"/>
                  <Trigger Property="IsPressed" Value="True"/>
                  <Trigger Property="IsEnabled" Value="False"/>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>

Hopefully one of those 2 will work for you.

你げ笑在眉眼 2024-10-15 12:30:25

仅查看您发布的代码,我看不到 TextBlock 样式如何以任何方式影响按钮的外观 - 除非按钮的内容(直接或间接)由 TextBlock 组成。您能否发布更完整的代码示例,可能包括按钮的 XAML?

Looking only ay the code you posted, I can't see how the TextBlock Style would in any way influence the appearance of the Buttons - unless the Content of the Buttons consists (directly or indirectly) of TextBlocks. Can you post a more complete code sample, possibly including the Button's XAML?

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