【问题标题】:Changing BackGround color of combobox when ItemsSource is set设置 ItemsSource 时更改组合框的背景颜色
【发布时间】:2016-01-03 13:45:34
【问题描述】:

我有一个 wpf c# 应用程序。

我正在使用组合框并将 ItemsSource 设置为我的 Observable 集合。

我希望组合框的背景不是灰色的。

此控件位于 UserControl 中。

<UserControl x:Class="InformedWorkerClient.UserControls.ucJobStatus"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:InformedWorkerClient.UserControls"
         mc:Ignorable="d" 
         d:DesignHeight="27" d:DesignWidth="300" Background="White">
<Grid Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>


    <ComboBox x:Name="cboJobStatus" Grid.Row="0" Grid.Column="0" Background="White"
        DisplayMemberPath="Description" 
        SelectedValuePath="JobStatusRef" 
        SelectedValue="{Binding Path=JobStatus}" SelectionChanged="cboJobStatus_SelectionChanged" />
</Grid>

补充:

这是选择项目后背景的样子。我希望背景是白色的

【问题讨论】:

标签: c# wpf combobox


【解决方案1】:

由于 ComboBox 的 ControlTemplate 中存在错误,您无法执行此操作。

可能的解决方案是复制/编辑模板并创建自己的模板。

这个问题是duplicate。也许那里的答案可以用更多信息进行更新。我搜索了wpf cant change background of combobox,最上面的链接是关于如何使用copy the template 的 MSDN 答案,但即便如此,该解决方案也充斥着不同 Windows 版本、航空主题的问题,并且无法解决用户开启高对比度的情况,和其他可访问性问题。

【讨论】:

  • 谢谢,我一回来就看看,如果问题解决了就删除。
【解决方案2】:

我建议使用&lt;ComboBox.ItemContainerStyle&gt; 并将背景设置为Transparent 作为容器。然后每个项目的背景将与ComboBox中的一样。

【讨论】:

  • 感谢您的回复。这不是我所追求的。我用一张图片扩充了我的问题,以使其更清晰。谢谢
猜你喜欢
  • 2016-07-09
  • 2019-05-15
  • 2016-07-31
  • 2018-12-26
  • 2014-09-17
  • 2022-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多