【发布时间】:2022-01-21 22:59:19
【问题描述】:
我有一个 WPF 项目,我想动态设置它的背景颜色,这是我的 XAML 代码
<Window.Resources>
<SolidColorBrush x:Key="TextBoxBorderColor" Color="#FFB4A5B4"/>
<SolidColorBrush x:Key="TextBoxForegroundColor" Color="Black"/>
<SolidColorBrush x:Key="TextBoxBackgroundColor" Color="White"/>
</Window.Resources>
<ComboBox x:Name="cmbUserFullName" Background="{DynamicResource TextBoxBackgroundColor}" Foreground="{DynamicResource TextBoxForegroundColor}"
BorderBrush="{DynamicResource TextBoxBorderColor}">
</ComboBox>
这是我的代码
var brush7 = FindResource("TextBoxBackgroundColor") as SolidColorBrush;
if (!string.IsNullOrEmpty(Default.clrPckerTextBoxBackground)) brush7.Color = (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBackground);
var brush8 = FindResource("TextBoxForegroundColor") as SolidColorBrush;
if (!string.IsNullOrEmpty(Default.clrPckerTextBoxForeground)) brush8.Color = (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxForeground);
var brush9 = FindResource("TextBoxBorderColor") as SolidColorBrush;
if (!string.IsNullOrEmpty(Default.clrPckerTextBoxBorder)) brush9.Color = (Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBorder);
但它不会改变组合框背景颜色和边框画笔颜色。 谁能帮我知道我的代码哪里出了问题? 谢谢...
【问题讨论】:
-
要添加或覆盖现有资源,您需要将值设置为资源本身,并插入从资源中获取的变量。在这种情况下, this.Resources["TextBoxBackgroundColor"] = new SolidColorBrush((Color)ColorConverter.ConvertFromString(Default.clrPckerTextBoxBackground));
-
我做了但是;它没有任何影响
标签: c# wpf combobox background