【问题标题】:Why only some child elements will have their properties defined by their parents为什么只有某些子元素的属性由其父元素定义
【发布时间】:2011-01-19 07:42:12
【问题描述】:
<Window x:Class="tests.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="Red" Foreground="Cyan">
    <StackPanel VerticalAlignment="Center">
        <Button>123</Button>
        <TextBlock>123</TextBlock>
        <TextBox>123</TextBox>
    </StackPanel>
</Window>

在上面的代码中,只有 TextBlock 会“继承”前景色和背景色。 Button 和 TextBox 不应该也有这些颜色吗?为什么他们没有? alt text http://img707.imageshack.us/img707/8014/5uslgmbzkbyurgwuwgqtzv2.png

【问题讨论】:

    标签: c# .net wpf datacontext


    【解决方案1】:

    这是因为Button 特意选择退出Background 值继承,以保持外观一致。详情请见en.csharp-online.net/WPF_Concepts—Property_Value_Inheritance

    【讨论】:

      【解决方案2】:
      <Button background="{Binding ElementName=stackPanel1,Path=Background}" .../>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-24
        • 2015-02-13
        • 1970-01-01
        • 2023-03-31
        • 2022-08-23
        • 2021-03-06
        • 1970-01-01
        相关资源
        最近更新 更多