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

查看:26
本文介绍了如何在 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 文件中:

In the .xaml file:

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 代替 List

编辑

它实现了 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

查看项目的变化

  1. CheckedListItem中实现INotifyPropertyChanged接口(每个setter都应该调用PropertyChanged(this, new PropertyChangedEventArgs())活动)
  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天全站免登陆