如何通过 C# 代码更改组合框的边框颜色?

How to change the border color of combo box by c# code?

我有组合框的样式,我想通过 C# 代码更改组合框的边框画笔颜色,这里是组合框边框画笔的 xaml 代码

 <Border Grid.ColumnSpan="2" Name="Border"
          BorderBrush="#e3e9ef" 
          CornerRadius="0" BorderThickness="1, 1, 1, 1" 
          Background="{StaticResource ComboBoxNormalBackgroundBrush}" /> 

我设置了边框名称,我想通过代码更改它,这是我的 C# 代码

 Border  myBorder1 = new Border();
              myBorder1.FindName("Border");
              myBorder1.BorderBrush = new SolidColorBrush(Color.FromRgb(255, 0, 0));

问题是它的机器人工作的代码有谁能帮我更改边框颜色吗?

使用您的边框名称,您可以设置颜色而无需像这样创建新边框,

XAML

 <Border Grid.ColumnSpan="2" Name="BorderSample"
          BorderBrush="#e3e9ef" 
          CornerRadius="0" BorderThickness="1, 1, 1, 1" 
          Background="{StaticResource ComboBoxNormalBackgroundBrush}" /> 

C#

BorderSample.BorderBrush = Brushes.SlateBlue;

在 XAML 文件中,您可以删除 BorderBrush 属性。

 <Border Grid.ColumnSpan="2" Name="BorderSample" 
      CornerRadius="0" BorderThickness="1, 1, 1, 1" 
      Background="{StaticResource ComboBoxNormalBackgroundBrush}" />

您可以在 .cs 文件中写入。

borderSample.BorderBrush = new SolidColorBrush(new Color { R = 0xe3, G = 0xe9, B = 0xef, A = byte.MaxValue });