WPF overrides setter properties

I use the style in my XAML for the label:

<Style x:Key="TreatEye" TargetType="Label">
        <Setter Property="Foreground" Value="#d1d1d1" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Canvas>                            
                        <TextBlock x:Name="retreatText" Canvas.Left="80" Canvas.Top="5" FontSize="16"  Text="Retreatment"/>                                                        
                        <TextBlock x:Name="bioinsulatorText" Canvas.Left="21" Canvas.Top="33" Text="Bioinsulator" />
                        <TextBlock x:Name="kxlText" Canvas.Left="21" Canvas.Top="70" Text="KXL Kit" />
                    </Canvas>
...

The problem I see is that the FontSize property for "reatreatText" is not overridden from the setter value of 30. This builds fine, but "reatreatText" is displayed at the end as size 30. Why is this value not redefined

Thanks in advance.

+3
source share
2 answers

Sorry, but I tried your code inside Kaxaml and it works as expected:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
  <Style x:Key="TreatEye" TargetType="Label">
        <Setter Property="Foreground" Value="#d1d1d1" />
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Label">
                    <Canvas>                            
                        <TextBlock x:Name="retreatText" Canvas.Left="80" Canvas.Top="5" FontSize="16" Text="Retreatment"/>                                                        
                        <TextBlock x:Name="bioinsulatorText" Canvas.Left="21" Canvas.Top="33" Text="Bioinsulator" />
                        <TextBlock x:Name="kxlText" Canvas.Left="21" Canvas.Top="70" Text="KXL Kit" />
                    </Canvas>
                </ControlTemplate>
        </Setter.Value>                
      </Setter>
     </Style>
    </Page.Resources>

  <Grid>  
    <Label Style="{StaticResource TreatEye}">Ejemplo</Label>
  </Grid>
</Page>

Result:

alt text http://img231.imageshack.us/img231/695/capture2p.png

+3
source

You need to set TemplateBinding to TextBlock.

<TextBlock x:Name="retreatText"
           Canvas.Left="80" 
           Canvas.Top="5" 
           FontSize="{TemplateBinding FontSize}" 
           Text="Retreatment"/> 

, .

0

Source: https://habr.com/ru/post/1728194/


All Articles