C# 更改a<;类型>;列表框背景色

C# 更改a<;类型>;列表框背景色,c#,wpf,listbox,C#,Wpf,Listbox,我有一个列表框,其中包含一个customer类型的列表。所以我的Listbox.Items不再是ListItem类型,而是customer类型 我在customer字段中有一个isActive标志,我想知道如果isActive是真的,我将如何将背景颜色设置为红色 目前我已经试过了,但它不起作用,因为我不能让客户键入ListBoxtem List<object> inactiveCustomers = new List<object>();

我有一个列表框,其中包含一个customer类型的列表。所以我的Listbox.Items不再是ListItem类型,而是customer类型

我在customer字段中有一个isActive标志,我想知道如果isActive是真的,我将如何将背景颜色设置为红色

目前我已经试过了,但它不起作用,因为我不能让客户键入ListBoxtem

            List<object> inactiveCustomers = new List<object>();
        foreach (Customer item in ListBoxCustomers.Items)
        {
            if (!item.IsActive)
            {
                inactiveCustomers.Add(item);
                int index = ListBoxCustomers.Items.IndexOf(item);
                ListBoxItem x = (ListBoxItem)ListBoxCustomers.Items[index];
                x.Background = Brushes.Red;
            }
        }
List inactiveCustomers=new List();
foreach(ListBoxCustomers.Items中的客户项目)
{
如果(!item.IsActive)
{
不活动客户。添加(项目);
int index=ListBoxCustomers.Items.IndexOf(item);
ListBoxItem x=(ListBoxItem)ListBoxCustomers.Items[索引];
x、 背景=画笔。红色;
}
}
编辑:
每当我取消选中活动客户的复选框时,我调用一个执行上述代码的方法。每当我取消选中活动复选框时,我都会遍历客户并显示所有客户,此时,我想更改非活动的背景色以区分哪些是不活动的

要获得
列表框项目
,您可以使用
列表框
项目容器生成器
。确保在加载完成后执行此操作,就像在加载的
事件处理程序中一样,因为在加载之前控件还不存在

void Window_Loaded(object sender, RoutedEventArgs e)
{
    List<object> inactiveCustomers = new List<object>();
    foreach (Customer item in ListBoxCustomers.Items)
    {
        if (!item.IsActive)
        {
            inactiveCustomers.Add(item);
            ListBoxItem x = ListBoxCustomers.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
            if (x != null)
                x.Background = Brushes.Red;
        }
    }
}
void Window\u加载(对象发送方,路由目标)
{
List inactiveCustomers=新列表();
foreach(ListBoxCustomers.Items中的客户项目)
{
如果(!item.IsActive)
{
不活动客户。添加(项目);
ListBoxItem x=ListBoxCustomers.ItemContainerGenerator.ContainerFromItem(item)作为ListBoxItem;
如果(x!=null)
x、 背景=画笔。红色;
}
}
}

有两种方法可以做到这一点。“正确”的WPF方法是在XAML中完成这一切:

<ListBox x:Name="ListBoxCustomers" ItemsSource="{Binding Customers}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsActive}" Value="False">
                    <Setter Property="Background" Value="Red" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

在代码隐藏中执行此操作的WinForms样式方法需要从列表框中获取容器。但是,由于UI虚拟化,并非所有必需的项都有容器。因此,此代码将仅更改当前可见项的容器。如果滚动列表框,则新项目可能具有或不具有您期望的设置(取决于回收规则)

List inactiveCustomers=new List();
foreach(ListBoxCustomers.Items中的客户项目)
{
如果(!item.IsActive)
{
不活动客户。添加(项目);
var container=ListBoxCustomers.ItemContainerGenerator.ContainerFromItem(item)作为ListBoxItem;
if(容器!=null)
container.Background=画笔.Red;
}
}

它设法通过它进行解析,因此这是一个加号,但是,x现在为空。所以它永远不会设置背景。请确保在列表框加载指定的数据后运行此操作。我以Window.Loaded为例,但您在加载数据时可能设置了不同的设置。除了预加载时间外,许多WPF ItemsControl子类(列表框)还利用UI虚拟化,这防止UI为屏幕外的项目创建一组内存中的UI元素。这是ContainerFromItem/ContainerFromIndex返回null的另一种情况。这是在XAML中完成这一切的一个很好的理由。如果您在codebehind中执行此操作,则必须手动检测ItemContainerGenerator何时创建新项目并手动应用设置。我应该在OP中提到这一点,当表单加载时,这是可以的,因为它仅显示所有活动客户。但是我有一个复选框来显示所有客户,然后我选择这个复选框,我想显示每个客户,而那些不活跃的客户将有不同的背景色。因此,在加载窗口后将调用我的ChangeColorOfInactiveCustomer()方法。感谢wpf示例,它工作得非常好!谢谢。
List<object> inactiveCustomers = new List<object>();
foreach (Customer item in ListBoxCustomers.Items)
{
    if (!item.IsActive)
    {
        inactiveCustomers.Add(item);
        var container =  ListBoxCustomers.ItemContainerGenerator.ContainerFromItem(item) as ListBoxItem;
        if (container != null)
            container.Background = Brushes.Red;
    }
}