如何在WPF中实现复选框的列表框? [英] How to Implement a ListBox of Checkboxes in WPF?

查看:496
本文介绍了如何在WPF中实现复选框的列表框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管在编写Winforms应用程序方面有一定的经验,但是WPF的模糊性"仍然使我无法从最佳实践和设计模式中受益.

Although somewhat experienced with writing Winforms applications, the... "vagueness" of WPF still eludes me in terms of best practices and design patterns.

尽管在运行时填充列表,但列表框显示为空.

Despite populating my list at runtime, my listbox appears empty.

我一直遵循这篇有用的文章中的简单说明徒劳无功.我怀疑我缺少某种DataBind()方法,在该方法中,我告诉列表框我已经完成了对基础列表的修改.

I have followed the simple instructions from this helpful article to no avail. I suspect that I'm missing some sort of DataBind() method where I tell the listbox that I'm done modifying the underlying list.

在我的MainWindow.xaml中,我有:

In my MainWindow.xaml, I have:

    <ListBox ItemsSource="{Binding TopicList}" Height="177" HorizontalAlignment="Left" Margin="15,173,0,0" Name="listTopics" VerticalAlignment="Top" Width="236" Background="#0B000000">
        <ListBox.ItemTemplate>
            <HierarchicalDataTemplate>
                <CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
            </HierarchicalDataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

在我的隐藏代码中,我有:

In my code-behind, I have:

    private void InitializeTopicList( MyDataContext context )
    {
        List<Topic> topicList = ( from topic in context.Topics select topic ).ToList();

        foreach ( Topic topic in topicList )
        {
            CheckedListItem item = new CheckedListItem();
            item.Name = topic.DisplayName;
            item.ID = topic.ID;
            TopicList.Add( item );
        }
    }

通过追踪,我知道其中填充了四个项目.

Which, by tracing through, I know is being populated with four items.

编辑

我已将TopicList更改为ObservableCollection.仍然不起作用.

I have changed TopicList to an ObservableCollection. It still doesn't work.

    public ObservableCollection<CheckedListItem> TopicList;

编辑#2

我进行了两项更改,对您有帮助:

I have made two changes that help:

在.xaml文件中:

ListBox ItemsSource="{Binding}"

在我填充列表之后的源代码中:

In the source code after I populate the list:

listTopics.DataContext = TopicList;

我正在获取一个列表,但是刷新它们时并不能自动更新复选框状态.我怀疑进一步阅读可以解决此问题.

I'm getting a list, but it's not automagically updating the checkbox states when I refresh those. I suspect a little further reading on my part will resolve this.

推荐答案

使用ObservableCollection<Topic>代替List<Topic>

修改

它实现了INotifyCollectionChanged接口,以便在添加/删除/修改项目时让WPF知道

it implements INotifyCollectionChanged interface to let WPF know when you add/remove/modify items

编辑2

由于您在代码中设置了TopicList,因此它应该是依赖项属性,而不是公共字段

Since you set TopicList in code, it should be a Dependency Property, not a common field

    public ObservableCollection<CheckedListItem> TopicList {
        get { return (ObservableCollection<CheckedListItem>)GetValue(TopicListProperty); }
        set { SetValue(TopicListProperty, value); }
    }
    public static readonly DependencyProperty TopicListProperty =
        DependencyProperty.Register("TopicList", typeof(ObservableCollection<CheckedListItem>), typeof(MainWindow), new UIPropertyMetadata(null));

编辑3

查看项目更改

    CheckedListItem中的
  1. 实现INotifyPropertyChanged接口(每个设置器应调用PropertyChanged(this, new PropertyChangedEventArgs(<property name as string>))事件)
  2. 或从DependencyObject派生CheckedListItem,并将NameIDIsChecked转换为依赖项属性
  3. 或完全更新它们(topicList[0] = new CheckedListItem() { Name = ..., ID = ... })
  1. implement INotifyPropertyChanged interface in CheckedListItem (each setter should call PropertyChanged(this, new PropertyChangedEventArgs(<property name as string>)) event)
  2. or derive CheckedListItem from DependencyObject, and convert Name, ID, IsChecked to dependency properties
  3. or update them totally (topicList[0] = new CheckedListItem() { Name = ..., ID = ... })

这篇关于如何在WPF中实现复选框的列表框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆