如何根据父控件的不透明度控制后代控件的不透明度?(how to control opacity of descendant controls based on parent control'

编程入门 行业动态 更新时间:2024-10-25 03:33:27
如何根据父控件的不透明度控制后代控件的不透明度?(how to control opacity of descendant controls based on parent control's opacity ?)

我创建了一个用户控件,网格内有网格和几个控件。 我已将Opacity设置为我的父网格的.1,并且我想将后代控件的不透明度设置为1.在XAML树架构中可以做到吗? 这是我的xaml代码:

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2" Background="DarkBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <StackPanel Orientation="Vertical" Opacity="1" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center"> <ProgressBar Name="progressBar" Foreground="White" IsIndeterminate="True" MinWidth="100" Height="25" VerticalAlignment="Center"/> <TextBlock Name="txtProgressText" FontSize="40" Margin="0,5,0,0" Text="Please wait while application is being initialized." TextAlignment="Left" TextWrapping="Wrap" /> </StackPanel> </Grid>

I have created a user control having grid and few controls inside grid. I have set Opacity to .1 of my parent grid and i want to set the descendant control's opacity to 1. Is is possible to do in XAML tree architecture ? Here is my xaml code:

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2" Background="DarkBlue" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <StackPanel Orientation="Vertical" Opacity="1" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center"> <ProgressBar Name="progressBar" Foreground="White" IsIndeterminate="True" MinWidth="100" Height="25" VerticalAlignment="Center"/> <TextBlock Name="txtProgressText" FontSize="40" Margin="0,5,0,0" Text="Please wait while application is being initialized." TextAlignment="Left" TextWrapping="Wrap" /> </StackPanel> </Grid>

最满意答案

不透明度是一个将在子树下传播的属性。

如果父不透明度为0.5且子透明度为0.2,则父级将以50%绘制,子级将使用父级50%的20%绘制...有点像那样只是为了更好地理解...我希望你能得到我: )

试试这个,你会看到:

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2"> <StackPanel Orientation="Vertical" Opacity="1" > ... <Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2"> <StackPanel Orientation="Vertical" Opacity="0.5"> ...

这两个将以不同的方式绘制,这意味着孩子不会从其父级获取不透明度值,而是计算自己的值。

如果你想控制内部子项的绘制方式,我建议你创建一个附加属性,在VisualTree中继承它的内容,当设置为true时,它将“underneathly”相关元素的不透明度改为1或者他们以前的旧价值。

这是一个例子:

public static bool GetChangeOpacity(DependencyObject obj) { return (bool)obj.GetValue(ChangeOpacityProperty); } public static void SetChangeOpacity(DependencyObject obj, bool value) { obj.SetValue(ChangeOpacityProperty, value); } // Using a DependencyProperty as the backing store for ChangeOpacity. This enables animation, styling, binding, etc... public static readonly DependencyProperty ChangeOpacityProperty = DependencyProperty.RegisterAttached("ChangeOpacity", typeof(bool), typeof(YourClass), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, PropertyChangedCallback)); private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) { FrameworkElement fe = dependencyObject as FrameworkElement; if (fe != null && (bool)args.NewValue) { fe.Tag = fe.Opacity; fe.Opacity = 1; } else if(fe != null && fe.Tag != null) { fe.Opacity = (double)fe.Tag; } }

Thank you guys for the valuable suggestions. But i have found way to resolve my issue by using brush for the parent grid control.

<Grid Name="busyIndicatorGrid" Visibility="Visible" Opacity=".2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Grid.Background> <SolidColorBrush Color="DarkBlue" Opacity="0.2"/> </Grid.Background> <StackPanel Orientation="Vertical" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center"> <ProgressBar Name="progressBar" Foreground="White" IsIndeterminate="True" MinWidth="100" Height="25" VerticalAlignment="Center"/> <TextBlock Name="txtProgressText" FontSize="40" Margin="0,5,0,0" Text="Please wait while application is being initialized." TextAlignment="Left" TextWrapping="Wrap" /> </StackPanel>

By doing so it will draw parent grid with the opacity i have defined in background property and it will have no effect on inside controls. all the controls will be drawn as usual without any opacity overriding effect which was set on parent grid.

更多推荐

本文发布于:2023-08-04 10:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1414063.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:明度   控件   不透   后代   control

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!