自定义按钮的前景色(ControlPresenter)

发布于 2024-10-24 04:17:37 字数 2925 浏览 1 评论 0原文

我正在尝试在 App.xaml 中定义全局按钮样式,并且它基本上按我的预期工作。但是,我只是不知道如何让前台正常工作。无论我做什么,我都会获得默认 TextBlock 的样式(它将颜色设置为白色)。

    <Style TargetType="{x:Type Button}">
        <Setter Property="Margin" Value="3, 5" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="FocusVisualStyle" 
                Value="{StaticResource ButtonFocusVisual}" />
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Padding" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="gridMainButton"
                          RenderTransformOrigin="0.5, 0.5">
                        <Grid.RenderTransform>
                            <ScaleTransform x:Name="scaleTransform" 
                                            CenterX="0.5"
                                            CenterY="0.5" />
                        </Grid.RenderTransform>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates" >
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation
                                              Storyboard.TargetName="scaleTransform"
                                              Storyboard.TargetProperty="ScaleX"
                                              Duration="0"
                                              To="0.85" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Ellipse x:Name="ellipse"
                                 HorizontalAlignment="Stretch"
                                 VerticalAlignment="Stretch"
                                 StrokeThickness="2"
                                 Stroke="{StaticResource standardBackground}"
                                 Fill="{StaticResource standardBackground}" />
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Margin="4, 8"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我想我可以将 ContentPresenter 更改为 TextBlock,这对于这个特定的应用程序来说没问题,但我正在寻找更通用的解决方案。

谢谢,
工作时间

I am attempting to define a global button style in App.xaml, and it's mostly working as I expect. However, I just cannot figure out how to get the Foreground to work correctly. No matter what I do, I am getting the style of the default TextBlock (which sets the color to White).

    <Style TargetType="{x:Type Button}">
        <Setter Property="Margin" Value="3, 5" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="FocusVisualStyle" 
                Value="{StaticResource ButtonFocusVisual}" />
        <Setter Property="Foreground" Value="Red" />
        <Setter Property="Padding" Value="5" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="gridMainButton"
                          RenderTransformOrigin="0.5, 0.5">
                        <Grid.RenderTransform>
                            <ScaleTransform x:Name="scaleTransform" 
                                            CenterX="0.5"
                                            CenterY="0.5" />
                        </Grid.RenderTransform>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates" >
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver" />
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <DoubleAnimation
                                              Storyboard.TargetName="scaleTransform"
                                              Storyboard.TargetProperty="ScaleX"
                                              Duration="0"
                                              To="0.85" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>

                        <Ellipse x:Name="ellipse"
                                 HorizontalAlignment="Stretch"
                                 VerticalAlignment="Stretch"
                                 StrokeThickness="2"
                                 Stroke="{StaticResource standardBackground}"
                                 Fill="{StaticResource standardBackground}" />
                        <ContentPresenter HorizontalAlignment="Center"
                                          VerticalAlignment="Center"
                                          Margin="4, 8"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

I guess I could change the ContentPresenter to a TextBlock, which would be ok for this particular application, but I'm looking for a more generic solution.

Thanks,
wTs

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

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

发布评论

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

评论(3

扎心 2024-10-31 04:17:37

就像 Markus Hütter 所说,问题可能是您定义了 TextBlock 的隐式样式,并且当 Button Content 设置为字符串时,TextBlock< /code> 将在模板中具有 ContentPresenter 的位置创建。这个 TextBlock 将拾取隐式样式,这就是您遇到此问题的原因。

您可以通过为 string 指定 DataTemplate 来禁用为字符串创建的 TextBlock 的隐式样式。将以下内容添加到 App.xaml

<Application ...>
    <Application.Resources>
        <DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib"
                      DataType="{x:Type sys:String}">
            <TextBlock Text="{Binding}">
                <TextBlock.Resources>
                    <Style TargetType="{x:Type TextBlock}"/>
                </TextBlock.Resources>
            </TextBlock>
        </DataTemplate>
        <!--...-->
    </Application.Resources>
</Application>

Like Markus Hütter said, the problem is probably that you have an implicit Style for a TextBlock defined and when the Button Content is set to a string, a TextBlock will be created where you have the ContentPresenter in the Template. This TextBlock will pick up the implicit Style and that's why you're getting this problem.

You can disable the implicit Style for a TextBlocks that is created in place for a string by specifying a DataTemplate for string. Add the following to App.xaml

<Application ...>
    <Application.Resources>
        <DataTemplate xmlns:sys="clr-namespace:System;assembly=mscorlib"
                      DataType="{x:Type sys:String}">
            <TextBlock Text="{Binding}">
                <TextBlock.Resources>
                    <Style TargetType="{x:Type TextBlock}"/>
                </TextBlock.Resources>
            </TextBlock>
        </DataTemplate>
        <!--...-->
    </Application.Resources>
</Application>
寂寞美少年 2024-10-31 04:17:37

您似乎正在使用文本块的自定义样式(如果您说前景色是白色),我们可以称之为StandardTextBlockStyle

在外部网格内的按钮样式内。包括这个:

<Grid x:Name="gridMainButton">
    <Grid.Resources>
        <Style TargetType="TextBlock" BasedOn="{StaticResource StandardTextBlockStyle}">
            <Setter Property="Foreground" Value="Black"/>
        </Style>
    </Grid.Resources>
    <!-- ...-->
</Grid>

这应该覆盖默认的 TextBlock 样式。

you seem to be using a custom style for textblock (if you say Foreground color is white) lets call this StandardTextBlockStyle.

Within your button style inside the outer grid. include this:

<Grid x:Name="gridMainButton">
    <Grid.Resources>
        <Style TargetType="TextBlock" BasedOn="{StaticResource StandardTextBlockStyle}">
            <Setter Property="Foreground" Value="Black"/>
        </Style>
    </Grid.Resources>
    <!-- ...-->
</Grid>

this should override the default TextBlock style.

葵雨 2024-10-31 04:17:37

我遇到了类似的问题,有一个全局 TextBlock 样式,我无法在需要自定义颜色/字体的其他控件中覆盖它。

基于 Fredrik HedbladPavlo Glazkov(来自此处)回复,以下解决方案对我有用:

<DataTemplate DataType="{x:Type System:String}">
        <TextBlock Text="{Binding}">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
                    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground),
                        FallbackValue={StaticResource Colors_ThemeForeground_SCBrush}, TargetNullValue={StaticResource Colors_ThemeForeground_SCBrush}}"/>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </DataTemplate>

谢谢,
RDV

I had similar issue, there was a global TextBlock style and I was not able to override it in other controls where custom color/font was needed.

Based on Fredrik Hedblad & Pavlo Glazkov (from here) responses, the following solution worked for me:

<DataTemplate DataType="{x:Type System:String}">
        <TextBlock Text="{Binding}">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
                    <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}, Path=(TextElement.Foreground),
                        FallbackValue={StaticResource Colors_ThemeForeground_SCBrush}, TargetNullValue={StaticResource Colors_ThemeForeground_SCBrush}}"/>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </DataTemplate>

Thanks,
RDV

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