简体   繁体   English

wpf属性继承

[英]wpf property inheritance

I want ask a question about that code its simple XAML code as show as below. 我想问一个关于该代码的简单XAML代码的问题,如下所示。

<Window x:Class="WpfApplication15.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Background="Orange" FontStyle="Italic">
<StackPanel>
    <Label>blo</Label>
    <Button>hi</Button>
    <Button>hi2</Button>
</StackPanel>
</Window>

So simply we can imagine, labels background orange but background color of button wont be orange will be as default, but all controls FontStyle will be italic, so question is that! 因此,我们可以简单地想象一下,将背景标记为橙色,但是将按钮的背景色设为橙色将作为默认值,但是所有控件FontStyle将变为斜体,所以问题就在于此! why fontstyle of all control under root affected that but button's background doesn't?? 为什么在根目录下所有控件的字体样式都会影响到按钮的背景却没有?

I suppose that the ControlTemplate of the button sets the Background-brush explicitely in respect to windows ui-guidelines. 我想按钮的ControlTemplate相对于Windows ui指南明确地设置了背景画笔。 Therefore property-inheritance is broken for the Control.BackgroundProperty . 因此, Control.BackgroundProperty属性继承被破坏了。

To prove it, maybe this tool will help. 为了证明这一点,也许这个工具会有所帮助。

This is the definition of the default button template , Microsoft_Windows_Themes:ButtonChrome is doing the trick 这是默认按钮模板的定义,Microsoft_Windows_Themes:ButtonChrome可以解决问题

                    <ControlTemplate TargetType="{x:Type Button}">
                    <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                    </Microsoft_Windows_Themes:ButtonChrome>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM