wpf - 覆盖自定义样式的属性

标签 wpf xaml button properties

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

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

如何更改 XAML 中的属性(例如 FontWeight、FontSize 等)?我试过这个:
<Button FontWeight="Bold" FontSize="30" Foreground="Red">
</Button>

在设计器 View 中,我看到了变化。但是在运行时这些更改不会应用。

经过一番调查,我也有一个适用于所有 TextBlock 的样式,如下所示:
<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="FontFamily" Value="Segoe UI Semibold" />
    <Setter Property="Foreground" Value="White" />
</Style>

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

如果我在空项目中使用上面的样式,则如下所示:

enter image description here

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

最佳答案

您可以通过 BasedOn 来实现.我给你举个例子。

<Window.Resources>
    <Style TargetType="ToggleButton" BasedOn="{StaticResource DefToggleButton}">
       <Setter Property="FontWeight" Value="Bold"/>
       <Setter Property="Content" Value="Some Cool Stuff"/>
           <Style.Triggers>
                <Trigger Property="IsChecked" Value="True">
                      <Setter Property="Content" Value="More Stuff"/>
                </Trigger>
           </Style.Triggers>
    </Style>
</Window.Resources>

在我的资源中,我有 DefToggleButton,现在在我的 xaml 文件中我可以设置任何 Property根据我的需要(在这种情况下是 FontWeightContent Property )。

关于wpf - 覆盖自定义样式的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47470770/

相关文章:

c# - 严格计时应用中的 MediaElement & DispatcherTimer

c# - 需要一个长期运行的模式或解决方案,该模式或解决方案需要循环执行并在每个线程完成后更新UI

wpf - 手动实例化 DataTemplate?

c# - uwp xaml 解析失败,类库 dll

javascript - 如何在 jQuery Mobile 中为按钮添加文本颜色

java - 使用按钮停止和开始循环

swift - UIButton 按钮标题在单击另一个按钮的标题时自行更改

c# - 样式自定义用户控件禁用状态

wpf - 删除用户控件上的焦点矩形

c# - ICollectionView获取特定对象