WPF里的Foreground能传给子控件吗

梁苏彬 2008-12-04 10:16:23
我想要在顶层窗口设置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>
...全文
351 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
梁苏彬 2008-12-04
  • 打赏
  • 举报
回复
就是不想逐个设置Foreground,想省点事。实际项目中有六七个弹出窗口,每个窗口上有n个控件
GTX280 2008-12-04
  • 打赏
  • 举报
回复
<SolidColorBrush x:Key="RedBrush" Color="Red"/>
对控件逐个设置Foreground
梁苏彬 2008-12-04
  • 打赏
  • 举报
回复
我用打了补丁的VS2008再试过了,情况还是一样。不过弄清了一件事,WPF的设计界面有bug,有时在设计时看到的效果并不准确,只能用编译运行后的效果为准。现在我想知道的就是怎么把Foreground传递到子控件。目前我测试只有TextBlock类型能接受父窗口的Foreground属性,TextBox\Label\CheckBox等都不行。
而当TextBlock被TabControl包在里面后,顶层窗口的属性也传不下来了。
梁苏彬 2008-12-04
  • 打赏
  • 举报
回复
使用了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>
LorenLiu 2008-12-04
  • 打赏
  • 举报
回复
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还是一个一个设置吧~~~

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧