简体   繁体   English

WPF中的透明边框以编程方式

[英]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: 生成在XAML文件中的视口上透明的边框(用于Trackball事件)是微不足道的:

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

But how do I do it in the .cs? 但是我如何在.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. 这是因为您不能仅将Color转换为Brush。 use the Transparent brush instead 请改用透明画笔

border.Background = Brushes.Transparent;

Use a SolidColorBrush : 使用SolidColorBrush

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

The VisualBrush has a different purpose. VisualBrush有不同的用途。 See an overview of the main types of WPF brushes here: 请参阅此处的WPF画笔主要类型概述:

http://msdn.microsoft.com/en-us/library/aa970904.aspx 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 您还可以使用透明颜色创建SolidColorBrush:这将创建完全透明的颜色

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: 但你也可以通过改变alpha来制作半透明颜色(这看起来像50%透明的红色:

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

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

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