开发者

Transparent border in WPF programmatically

开发者 https://www.devze.com 2023-04-08 20:31 出处:网络
It\'s trivial to generate a border (to use for Trackball events) transparent over the viewport in the XAML file:

It's trivial to generate a border (to use for Trackball events) transparent over the viewport in the XAML file:

<Border Name="myElement" Background="Transparent" />

But how开发者_运维问答 do I do it in the .cs?

Border border = new Border();
**border.Background = (VisualBrush)Colors.Transparent;**
grid.Children.Add(viewport);
grid.Children.Add(border);

This does not work of course.


This is because you can't just cast a Color to be a Brush. use the Transparent brush instead

border.Background = Brushes.Transparent;


Use a SolidColorBrush:

border.Background = new SolidColorBrush(Colors.Transparent);

The VisualBrush has a different purpose. See an overview of the main types of WPF brushes here:

http://msdn.microsoft.com/en-us/library/aa970904.aspx


You can also create a SolidColorBrush with transparent color: This will create a fully transparent color

border.Background = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));

but you can also make semitransparent color by changing alpha (this will look like 50% transparent red:

border.Background = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));
0

精彩评论

暂无评论...
验证码 换一张
取 消