WPF 重写 setter 属性

发布于 2024-08-18 07:39:58 字数 1057 浏览 5 评论 0原文

我在 XAML 中使用样式作为标签:

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

我看到的问题是“reatreatText”的 FontSize 属性未从 setter 值 30 覆盖。这构建得很好,但最终显示有“reatreatText”大小为 30。为什么这个值没有被覆盖?

提前致谢。

I'm using a style in my XAML for a 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'm seeing is that the FontSize property of "reatreatText" is not overridden from the setter value of 30. This builds fine, but the end display has "reatreatText" as size 30. Why is this value not overridden?

Thanks in advance.

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

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

发布评论

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

评论(2

夏日落 2024-08-25 07:39:58

抱歉,但我在 Kaxaml 中尝试了您的代码并按预期工作:

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

结果:

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

Sorry, but I tried your code inside Kaxaml and 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

甜嗑 2024-08-25 07:39:58

您需要在 TextBlock 上设置 TemplateBinding。

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

这就是 setter 属性传播到内部结构的方式。

You need to set a TemplateBinding on the TextBlock.

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

That's how the setter properties get propogated to the internal structure.

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