我想要在顶层窗口设置Foreground属性,希望里面的控件都用这个属性,能不能做到?经过我测试并不是不可以,但是属性不能很好的传递。如下面的窗口,两个TextBlock的字体颜色变成红色了,但CheckBox的颜色没变。在窗口里使用了TabControl后情况更奇怪,有时有效有时无效,有时在设计窗口能看到颜色,但一编译又没了。我用的是vs2008没有打补丁。
<Window x:Class="sandbox.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Foreground="Red">
    <Grid>
        <StackPanel>
            <TextBlock>TextBlock1</TextBlock>
            <TextBlock>TextBlock3</TextBlock>
            <CheckBox>CheckBox1</CheckBox>
        </StackPanel>
    </Grid>
</Window>

解决方案 »

  1.   

    使用了TabControl的代码
    <Window x:Class="sandbox.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300" Foreground="Red">
        <Grid>
           <TabControl Grid.Row="0">
                <TabItem>
                    <TabItem.Header>
                        General
                    </TabItem.Header>
                    <TabItem.Content>
                        <Grid >
                            <Grid.RowDefinitions>
                                <RowDefinition x:Uid="RowDefinition_3" Height="30"/>
                                <RowDefinition x:Uid="RowDefinition_4" Height="30"/>
                                <RowDefinition x:Uid="RowDefinition_5" Height="90"/>
                                <RowDefinition x:Uid="RowDefinition_6" Height="Auto"/>
                            </Grid.RowDefinitions>
                            <StackPanel Orientation="Horizontal"  VerticalAlignment="Center" Grid.Row="0">
                                <TextBlock Text="Server Name/IP:" Margin="10,1,3,0"/>
                                <TextBox x:Name="Server" Width="150" Margin="5,0,3,0"  Text="test"/>
                            </StackPanel>
                            <StackPanel Orientation="Horizontal"  VerticalAlignment="Center" Grid.Row="1">
                                <TextBlock Text="Server Port:" Margin="34,1,3,0"/>
                                <TextBox x:Name="ServerPort" Width="150" Margin="5,0,3,0"  Text="test"/>
                            </StackPanel>
                            <GroupBox Header="Work Mode" Grid.Row="2" Background="Transparent" Height="80">
                                <StackPanel Orientation="Horizontal">
                                    <CheckBox x:Name="ChkMode" Content="Work Offline" Height="20" FontSize="14" Margin="10,1"></CheckBox>
                                    <CheckBox x:Name="ChkSynced" Content="Force Database synchronization"  Margin="10,1" Height="20" FontSize="14"></CheckBox>
                                </StackPanel>
                            </GroupBox>
                        </Grid>
                    </TabItem.Content>
                </TabItem>
            </TabControl>
        </Grid>
    </Window>
      

  2.   

    我用打了补丁的VS2008再试过了,情况还是一样。不过弄清了一件事,WPF的设计界面有bug,有时在设计时看到的效果并不准确,只能用编译运行后的效果为准。现在我想知道的就是怎么把Foreground传递到子控件。目前我测试只有TextBlock类型能接受父窗口的Foreground属性,TextBox\Label\CheckBox等都不行。
    而当TextBlock被TabControl包在里面后,顶层窗口的属性也传不下来了。
      

  3.   

    <SolidColorBrush x:Key="RedBrush" Color="Red"/>
    对控件逐个设置Foreground
      

  4.   

    就是不想逐个设置Foreground,想省点事。实际项目中有六七个弹出窗口,每个窗口上有n个控件
      

  5.   

    MSDN上是这么写的
    This property only affects a control whose template uses the Foreground property as a parameter. On other controls, this property has no impact.
    所以对TextBlock可以起作用而CheckBox, Button一类的就不起作用。我们在使用Button的Foreground属性时,实际上也只是对Button内部的TextBlock的Foreground产生了作用,用Snoop查看Button的VisualTree可以发现
    Button
      Chrome
        ContentPresenter
          TextBlock也就是Button的内容(字符串)实际上是在TextBlock中被显示出来,并且Foreground属性也是被应用到了TextBlock上。所以LZ还是一个一个设置吧~~~