WPF - 使用可检查和可选择的 ListViewItems 扩展 ListView

标签 wpf xaml listview checkbox

我已经阅读了许多有关使用与 IsSelected 绑定(bind)的复选框扩展 ListView 的示例。但我想要更多。

我想要在选中状态和选中状态之间进行分离,因此我得到一个具有单个选定项目的列表框,但可以有多个选中项目。 不幸的是,ListViewItem 没有检查属性,并且我看不到让 ListView 与自定义 CheckableListViewItem 一起使用的可能性。

当然,我可以使用带有已检查属性的对象列表作为 ItemSource,但我认为这不是一个好方法。检查与否是列表或项目容器的问题,而不是其中列出的对象的问题。除此之外,我不希望我的所有类(如用户、角色、组)都有对应的类,如 checkableUser、checkableRole 和 checkableGroup。

我想要的行为可以通过 UI 轻松实现

<DataTemplate x:Key="CheckBoxCell">
   <StackPanel Orientation="Horizontal">
      <CheckBox />
   </StackPanel>
</DataTemplate>

和一个

<GridViewColumn CellTemplate="{StaticResource CheckBoxCell}" Width="30"/>

但是如果没有绑定(bind)复选框,我无法检查它是否被选中。

有什么办法可以完成这样的事情吗?对我来说,完美的解决方案是拥有 listView1.SelectedItem、listView1.CheckedItems,也许还有 listView1.UncheckedItems,当然还有 listView1.CheckItem 和 listView1.UncheckItem。

感谢您的帮助。

最佳答案

好的,我明白了。 没什么可做的,但因为我对整个 WPF 东西都很陌生,所以需要做一些工作来弄清楚。 解决办法如下:

public class CheckableListViewItem : ListViewItem
{
    [Category("Appearance")]
    [Bindable(true)]
    public bool IsChecked { get; set; }
}

public class CheckableListView : ListView
{
    public IList CheckedItems
    {
        get
        {
            List<object> CheckedItems = new List<object>();
            for (int i=0;i < this.Items.Count; ++i)
            {
                if ((this.ItemContainerGenerator.ContainerFromIndex(i) as CheckableListViewItem).IsChecked)
                    CheckedItems.Add(this.Items[i]);
            }
            return CheckedItems;
        }
    }
    public bool IsChecked(int index)
    {
        if (index < this.Items.Count) return (this.ItemContainerGenerator.ContainerFromIndex(index) as CheckableListViewItem).IsChecked;
        else throw new IndexOutOfRangeException();
    }
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        if (item is CheckableListViewItem) return true;
        else return false;
    }
    protected override DependencyObject GetContainerForItemOverride()
    {
        return new CheckableListViewItem();
    }
}

插入 Window.Resources 下的 XAML(clr = 我的类命名空间):

<DataTemplate x:Key="CheckBoxCell">
    <StackPanel Orientation="Horizontal">
        <CheckBox IsChecked="{Binding Path=IsChecked, 
            RelativeSource={RelativeSource FindAncestor, 
            AncestorType={x:Type clr:CheckableListViewItem}}}" />
    </StackPanel>
</DataTemplate>

这就是您的 CheckableListView:

<clr:CheckableListView SelectionMode="Single" [...] >
        <ListView.View>
            <GridView>
                <GridViewColumn CellTemplate="{StaticResource CheckBoxCell}" 
                      Width="30"/>
                [...]
            </GridView>
        </ListView.View>
    </clr:CheckableListView>

也许这可以帮助遇到同样问题的人。

关于WPF - 使用可检查和可选择的 ListViewItems 扩展 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2774884/

相关文章:

WPF 工具提示样式

c# - 为什么这段代码似乎有错误?

c# - 具有不同类别下的子节点的 WPF TreeView

wpf - 无法识别目标名称

c# - 如何调整画笔纵横比

android - 为什么在 ListView 中使用 Inflater

wpf - ListView XAML 中的替代背景颜色

wpf - 组合框鼠标悬停

.net - XAML 性能 : Set alpha channel of a brush vs. FrameworkElement 的不透明度

java - 使用 BaseAdapter 重新排序和重复 ListView 项目