覆盖自定义样式的属性

el-*_*ino 6 wpf xaml properties button

我的Style适用于我的应用程序的所有按钮:

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
    <Setter Property="Background" Value="Red" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="FontSize" Value="16" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid>
                    <Ellipse x:Name="StatusButtonCircle" Stroke="Black" StrokeThickness="0" Fill="AliceBlue" Stretch="Uniform">
                        <Ellipse.Width>
                            <Binding ElementName="StatusButtonCircle" Path="ActualHeight"/>
                        </Ellipse.Width>
                    </Ellipse>
                    <Ellipse x:Name="StatusButtonCircleHighlight" Margin="4" Stroke="Black" StrokeThickness="2" Stretch="Uniform">
                        <Ellipse.Width>
                            <Binding ElementName="StatusButtonCircleHighlight" Path="ActualHeight"/>
                        </Ellipse.Width>
                    </Ellipse>
                    <ContentPresenter HorizontalAlignment="Center"  
                                    VerticalAlignment="Center"/>
                </Grid>

                <ControlTemplate.Triggers>
                    ... some Triggers here
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

如何在XAML中更改属性(例如FontWeight,FontSize等)?我试过这个:

<Button FontWeight="Bold" FontSize="30" Foreground="Red">
</Button>
Run Code Online (Sandbox Code Playgroud)

在设计师视图中,我看到了变化.但在运行期间,这些更改不会应用.


经过一番调查,我也有一个Style for all TextBlock,如下所示:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="FontFamily" Value="Segoe UI Semibold" />
    <Setter Property="Foreground" Value="White" />
</Style>
Run Code Online (Sandbox Code Playgroud)

此样式似乎覆盖了Button上使用的TextBlock.我仍然无法更改XAML中的文本属性.

如果我在空项目中使用上面的样式,这就是它的样子:

在此输入图像描述

在设计器中,应用更改,在运行时,应用TextBlock中的更改.如果我将ax:Key分配给TextBlock,它可以正常工作.但是我必须手动将此样式分配给应用程序中使用的每个TextBlock.

小智 2

您面临着 wpf 中典型的样式继承问题。

控件在初始化时会查找其样式。控件查找其样式的方式是在逻辑树中向上移动,并询问逻辑父级是否有适合它们的样式存储在父级的资源字典中。

在您的情况下,您在按钮中使用 ContentPresenter 作为默认行为。默认情况下,它使用 TextBlock 来表示按钮中的文本。

因此在初始化时,ContentPresenter找到TextBlock样式并应用来表示按钮中的内容。

如果您想限制 ContentPresenter 查找样式,那么您必须将空白样式绑定到内容演示器,以便它不会查找任何其他样式。

<Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
            <Setter Property="Background" Value="Red" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="FontSize" Value="16" />

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Grid>
                            <Ellipse x:Name="StatusButtonCircle" Stroke="Black" StrokeThickness="0" Fill="AliceBlue" Stretch="Uniform">
                                <Ellipse.Width>
                                    <Binding ElementName="StatusButtonCircle" Path="ActualHeight"/>
                                </Ellipse.Width>
                            </Ellipse>
                            <Ellipse x:Name="StatusButtonCircleHighlight" Margin="4" Stroke="Black" StrokeThickness="2" Stretch="Uniform">
                                <Ellipse.Width>
                                    <Binding ElementName="StatusButtonCircleHighlight" Path="ActualHeight"/>
                                </Ellipse.Width>
                            </Ellipse>
                            <ContentPresenter HorizontalAlignment="Center"  
                                    VerticalAlignment="Center">
                                <ContentPresenter.Resources>
                                    <Style TargetType="TextBlock" BasedOn="{x:Null}"/>
                                    <!--  Assigned Blank style here therefore it will not search for any further style-->
                                </ContentPresenter.Resources>
                            </ContentPresenter>
                        </Grid>


                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
Run Code Online (Sandbox Code Playgroud)