简体   繁体   English

WPF将背景设置为透明和在控件上设置不透明度有什么区别?

[英]WPF What is the difference between setting the background to transparent and setting opacity on the control?

So I realized that my graph viewer had the axis displaying over the actual items in the graph, so I changed the ZIndex on the grid to display the items over the axis instead. 所以我意识到我的图形查看器的轴显示在图形中的实际项目上,因此我更改了网格上的ZIndex以显示轴上的项目。

However, I noticed that I couldn't actually see anything under the actual items because the background of the items were opaque. 但是,我注意到我实际上看不到任何物品,因为物品的背景是不透明的。 I think I have two options then, to either set the background of the items to transparent, or to set the opacity of the items. 我想我有两个选项,要么将项目的背景设置为透明,要么设置项目的不透明度。 Is there any difference between these two options? 这两个选项有什么区别吗?

              <Grid 
                Grid.Row="0" 
                Grid.RowSpan="2"
                Grid.Column="0" 
                Grid.ZIndex="1"
                >
                <Components:SignalGraphAxis 
                  x:Name="signal_axis"
                  VerticalAlignment="Stretch"
                  HorizontalAlignment="Stretch"
                  GraphHeight="{Binding Path=GraphHeight}"
                  PenColor="{Binding Path=AxisColor, Mode=OneWay}"
                  PenWidth="{Binding Path=GraphPenWidth, Mode=OneWay}"
                  MinHeight="10"
                  MinWidth="10"
                  AxisTimeScale="{Binding Path=GraphTimeScale}"
                  NumberOfPixelsPerDivision="{Binding Path=NumberOfPixelsPerDivision, Mode=OneWay}"
                  MinDisplayValue ="{Binding Path=MinDisplayValue, Mode=OneWay}"
                  UnitsOfGraphTimePerInch="{Binding Path=UnitsOfTimePerInch, Mode=OneWay}"
                  />
              </Grid>
              <ScrollViewer
                x:Name="signal_scrollviewer"
                Grid.Row="1"
                Grid.RowSpan="2"
                Grid.Column="0"
                Grid.ColumnSpan="2"
                Grid.ZIndex="2"
                VerticalAlignment="Stretch" 
                HorizontalAlignment="Stretch"
                HorizontalContentAlignment="Left"
                HorizontalScrollBarVisibility="Visible"
                VerticalScrollBarVisibility="Visible"
                CanContentScroll="True"
                Style="{StaticResource SignalScrollViewerStyle}"
                >

                <ItemsPresenter />
              </ScrollViewer>
            </Grid>

Background property is defined on Control class and Opacity is defined much higher on UIElement . Background属性在Control类上定义, OpacityUIElement上定义得更高。

From MSDN Page Control.Background Property 来自MSDN Page Control.Background属性

This property only affects a control whose template uses the Background property as a parameter. 此属性仅影响其模板使用Background属性作为参数的控件。 On other controls, this property has no impact. 在其他控件上,此属性没有任何影响。

Let's try to create a Custom Control to see how this works. 让我们尝试创建一个自定义控件,看看它是如何工作的。

CustomControl1.cs CustomControl1.cs

public class CustomControl1 : ContentControl
{
    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }
}

Default Template For CustomControl1 CustomControl1的默认模板

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="My Custom Control " Grid.Row="0" />
                    <ContentPresenter Grid.Row="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Please note, the above template doesn't use Background property at all in it's Template . 请注意,上面的模板在它的Template根本不使用Background属性。

Now, Let's try to use that in a Form and see how it behaves: 现在,让我们尝试在表单中使用它,看看它的行为:

Code from Window1.xaml 来自Window1.xaml的代码

<Grid>
    <wpfApplication5:CustomControl1 Background="Green">
        <Button Content="Button Within Custom Control" Margin="25"/>
    </wpfApplication5:CustomControl1>
</Grid>

The resultant output: 结果输出:

在此输入图像描述

See, there was no Green background for the rendered CustomControl even though we set the Background to Green in Window1.xaml. 请参阅,即使我们在Window1.xaml中将Background设置为Green ,渲染的CustomControl也没有绿色背景。

Now, Lets modify the template to use Background property. 现在,让我们修改模板以使用Background属性。

Template with Background Property 模板与背景属性

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <TextBlock Text="My Custom Control " Grid.Row="0" />
                    <ContentPresenter Grid.Row="1" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

And the resultant output will now have a the specified background applied. 现在,结果输出将应用指定的背景。

在此输入图像描述

I think (couldn't find any references though), Opacity gets applied to the element/Control whether the Control's Template worry about Opacity property or not. 我认为(虽然找不到任何引用),Opacity会应用于元素/ Control,无论Control的模板是否担心Opacity属性。

Window1.xam with Opacity Set on CustomControl 在CustomControl上设置Opacity的Window1.xam

<Grid>
    <wpfApplication5:CustomControl1 Background="Green" Opacity="0.2">
        <Button Content="Button Within Custom Control" Margin="25"/>
    </wpfApplication5:CustomControl1>
</Grid>

and resultant Output 和结果输出

在此输入图像描述

See, the Opacity got applied even though our Custom Control's template doesn't worry anything about Opacity property. 请参阅,即使我们的自定义控件的模板不担心Opacity属性,也会应用Opacity

Finally, to answer your question: Though either setting Opacity to 0 or Background to Transparent may give you the same visual result. 最后,回答你的问题:虽然将Opacity设置为0Background to Transparent可能会给你相同的视觉效果。 But, for Background property, it totally depends on the Control implementation and how it handles Background property. 但是,对于Background属性,它完全取决于Control实现以及它如何处理Background属性。 Whereas, with Opacity it gets applied from parent elements to down the elements tree to child elements irrespective of the control. 然而,对于Opacity ,无论控件如何,它都从父元素应用到元素树到子元素。

Refer to MSDN Page, UIElement.Opacity Property to read more on Opacity property and how it behaves when Opacity is set at multiple levels in an element tree. 请参阅MSDN页面, UIElement.Opacity属性以了解有关Opacity属性的更多信息以及在元素树中将不透明度设置为多个级别时的行为方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式设置WPF背景Opacity - Setting WPF background Opacity programmatically 在WPF图像控件上设置背景图像? - Setting a background image on a WPF image control? 将MasterDetailPage的背景设置为透明 - Setting the background of a MasterDetailPage to be transparent 显式设置不透明度后,如何通过IsEnabled重置WPF控件的默认不透明度切换? - How to reset a WPF control's default opacity toggling via IsEnabled after setting opacity explictly? Silverlight / WPF中的Control和ContentControl有什么区别? - What is the difference between Control and ContentControl in Silverlight/WPF? * any * wpf控件的动画背景不透明度 - Animate background opacity of *any* wpf control 设置窗口图像背景的不透明度属性 - Setting Opacity property for Window Image Background 将背景和不透明度设置为按钮但不影响其内容 - Setting background & opacity to button but not affecting it's content 在构造函数中设置DataContext = this和在WPF中绑定到{RelativeSource Self}之间的区别? - Difference between setting DataContext=this in constructor and Binding to {RelativeSource Self} in WPF? 设置控制父属性和使用Controls.Add()之间的区别? - Difference between setting control Parent property and using Controls.Add()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM