简体   繁体   English

改变一个 <Type> 列表框背景色

[英]Changing a <Type> Listbox backcolor

I have a Listbox that holds a list of type customer. 我有一个Listbox,其中包含customer类型列表。 So my Listbox.Items is not of type ListItem anymore, it is of type customer. 所以我的Listbox.Items不再是ListItem类型,它是customer类型。

I have an isActive flag in the customer field and I was wondering how I would set the back color say to red, if the isActive is true. 我在客户领域有一个isActive标志,如果isActive为真,我想知道如何将背面颜色设置为红色。

Currently I have tried this but it doesnt work since I can't cast Customers to type ListBoxtem 目前我已经尝试过这个但它不起作用,因为我无法将客户转换为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;
            }
        }

EDIT: I am calling a method that does the above code whenever I deselect a checkbox that is for active customers. 编辑:每当我取消选择一个活跃客户的复选框时,我正在调用一个执行上述代码的方法。 Whenever I deselect the Active Checkbox, I iterate through the customers and display all of them, and at this time, I would like to change the backcolor of the inactive to distinguish which ones are inactve/active 每当我取消选择Active Checkbox时,我会遍历客户并显示所有客户,并且此时,我想更改非活动的背景颜色以区分哪些是不活动/活动的

There are two ways to do this. 有两种方法可以做到这一点。 The "correct" WPF way is to do it all in XAML: “正确的”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>

The WinForms-style way to do this in code-behind requires getting the container from the listbox. 在代码隐藏中执行此操作的WinForms风格方法需要从列表框中获取容器。 However, due to UI virtualization, not all items necessary have a container. 但是,由于UI虚拟化,并非所有必需的项目都有容器。 Therefore, this code will only change the containers for the the current visible items. 因此,此代码仅更改当前可见项的容器。 If the ListBox is scrolled, the new items may or may not have the setting you expect (depending on recycling rules). 如果滚动列表框,则新项目可能有也可能没有您期望的设置(取决于回收规则)。

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;
    }
}

To get the ListBoxItem you can use the ListBox 's ItemContainerGenerator . 要获取ListBoxItem您可以使用ListBoxItemContainerGenerator Make sure you do this after loading has completed, like in a Loaded event handler, since the controls don't exist yet before loading. 确保在加载完成后执行此操作,就像在Loaded事件处理程序中一样,因为在加载之前控件尚不存在。

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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM