Override custom style property

I have a style that applies to all buttons of my application:

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

How to change properties (e.g. FontWeight, FontSize, etc.) in XAML? I tried this:

<Button FontWeight="Bold" FontSize="30" Foreground="Red">
</Button>

In the view of the constructor, I see changes. But at runtime, these changes are not applied.


After some research, I also have a style for all TextBlocks:

<Style TargetType="{x:Type TextBlock}">
    <Setter Property="FontSize" Value="16" />
    <Setter Property="FontFamily" Value="Segoe UI Semibold" />
    <Setter Property="Foreground" Value="White" />
</Style>

This style seems to override the TextBlock that is used on the button. I still cannot change the text properties in XAML.

Here's what it looks like if I use Styles above in an empty project:

enter image description here

, TextBlock. x: Key TextBlock, . , , .

+6
4

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

, Template Style, , , :

<Window.Resources>
    <Style TargetType="Button" x:Key="stBtn>
        <Setter Property="Background" Value="Blue" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="FontFamily" Value="Segoe UI Semibold" />
    </Style>
</Window.Resources>

Template, , Button Border ContentPresenter , .

Template Button :

<Button Content="Hi!" Style="{StaticResource stBtn}" Foreground="Red" >

, Button Red Foreground.

=================

Edit

, , Template , ?

, TemplateBinding, , Foreground , .

<Window.Resources>
    <ControlTemplate x:Key="ctBtn" TargetType="{x:Type Button}">
        <Label Background="Green" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}"/>
    </ControlTemplate>

    <Style x:Key="stBtn2" TargetType="{x:Type Button}">
        <Setter Property="Template"
            Value="{StaticResource ctBtn}" />
    </Style> 
<Window.Resources>

, Button:

<Button Content="Hi!" Style="{StaticResource stBtn2}" Foreground="Red" >

===============

Edit2

, , TemplateBinding . , Ellipse :

<Ellipse Fill="{TemplateBinding BorderBrush}" /> 

, Fill BorderBrush (, )

, Label Template TemplateBinding Forground FontWeight.

<Label Foreground="{TemplateBinding Foreground}" />   
0

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 ( FontWeight Content Property).

0

-, , , Style ResourceDictionary, Application.Resources (preciselly TextBlock ). Style , , Window.Resources .

TextBlock TextBlock, ConentPresenter

, , (keyless) Style TextBlock TextBlock, ContentPresenter, , . - , <<20 > Window.Resources. , , " ".

ControlTemplate - , Control

TextBlock ( Control, UIElement) UIElement), , wpf Style . , Style , Style Style Application.Resources.

( FrameworkElement, ), , , . , Button ( ), , , ControlTemplate s. , , Button Control. , , TextBlock , TextBlock Style. ComboBox, Label..., TextBlock, Button.

, : Style , Control Application.Resources, 100% , , ( Window.Resources ). , , MahApps.Metro UI: " TextBlock App.xaml!!!". TextBlock Button ControlTemplate, Label, ComboBox .. , .

0

All Articles