您的位置:首页 > 其它

wpf 父控件透明子控件不透明

2015-06-25 18:54 309 查看
在wpf开发中遇到子控件会继承父类控件属性的问题,

例如:

<StackPanel  Orientation="Horizontal" Grid.Row="1" Background="Black" Opacity="0.3">
<Label Content="{Binding Id}"  Foreground="White"  VerticalAlignment="Center"></Label>
<Label Content="{Binding Name}" Foreground="White" VerticalAlignment="Center"></Label>
</StackPanel>


此时两个Label都会继承stackPanel的Opacity属性,如果想要不继承透明属性,这时候需要用到笔刷,代码如下:

<SolidColorBrush Color="Black" Opacity=".3" x:Key="OpacityBrush"/>

<StackPanel  Orientation="Horizontal" Grid.Row="1" Background="{StaticResource OpacityBrush}">
<Label Content="{Binding Id}"  Foreground="White"  VerticalAlignment="Center"></Label>
<Label Content="{Binding Name}" Foreground="White" VerticalAlignment="Center"></Label>
</StackPanel>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: