简体   繁体   中英

Transparent border in WPF programmatically

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM