如何在XAML中设置背景不透明度和边框不透明度?

时间:2011-10-12 16:48:19

标签: c# wpf xaml

我有TextBox

<TextBox x:Name="myTextBox"/>

后面的代码TextBox有两个布尔值:

 myTextBox.Background.Opacity = 0;
 myTextBox.BorderBrush.Opacity = 0;

现在这一切都很好,但我如何在XAML中设置这两个属性?

顺便说一下,设置:

<TextBox x:Name="myTextBox" Background="#00FFFFFF"/>

不影响Opacity属性。我想在XAML中专门设置opacity属性。

4 个答案:

答案 0 :(得分:51)

你想做这样的事情:

<TextBlock Text="foo bar">
    <TextBlock.Background>
        <SolidColorBrush Color="Azure" Opacity="0.5" />
    </TextBlock.Background>
</TextBlock>

答案 1 :(得分:14)

XAML中的不透明度定义为double,而不是HTML颜色三元组。

http://msdn.microsoft.com/en-us/library/system.windows.uielement.opacity.aspx

您需要将其设置为:

<TextBlock Opacity="0" />

您也可以使用画笔进行设置:

<SolidColorBrush Color="#FF295564" Opacity="0.3"/>

...然后将背景属性设置为画笔。

答案 2 :(得分:3)

如果您只想在XAML中使用透明背景,则会有透明预设:

<Border Background="Transparent"/>

答案 3 :(得分:2)

I don't know when or if this was changed in the past, but at least with WPF 4.5 it's perfectly fine to use 8-digit-hex-color-codes:

<Element Background="#19ff0000"/> // background will be red with an alpha of 10%

The first two digits specify the alpha channel, with 00 (0) beeing fully transparent and FF (255) beeing fully opaque.