[英]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
类上定义, Opacity
在UIElement
上定义得更高。
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
设置为0
或Background
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.