【问题标题】:Transparent border in WPF programmaticallyWPF中的透明边框以编程方式
【发布时间】:2011-09-28 15:23:03
【问题描述】:

在 XAML 文件中的视口上生成透明的边框(用于跟踪球事件)很简单:

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

但是我如何在 .cs 中做到这一点?

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

这当然行不通。

【问题讨论】:

    标签: c# wpf xaml wpf-controls


    【解决方案1】:

    这是因为您不能只将颜色转换为画笔。请改用Transparent 笔刷

    border.Background = Brushes.Transparent;
    

    【讨论】:

      【解决方案2】:

      使用SolidColorBrush

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

      VisualBrush 有不同的用途。在此处查看 WPF 画笔主要类型的概述:

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

      【讨论】:

        【解决方案3】:

        您还可以创建一个具有透明颜色的 SolidColorBrush: 这将创建一个完全透明的颜色

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

        但您也可以通过更改 alpha 来制作半透明颜色(这看起来像 50% 透明的红色:

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

        【讨论】:

          猜你喜欢
          • 2018-05-18
          • 2023-03-08
          • 1970-01-01
          • 2013-05-31
          • 2013-06-18
          • 2013-06-12
          • 2011-05-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多