WPF - ListView Extension with Inspect and Select ListViewItems

I have already read many examples on extending ListViews with checkboxes associated with IsSelected. But I want something else.

I want separation between checked and selected state, so I end up with a ListBox that has one selected item, but can have multiple checked items. Unfortunately the ListViewItem does not have a property to check, and I see no way to get the ListView to work with a custom CheckableListViewItem.

Of course, I could use a Checked Object List as the ItemSource, but I don't think that is a good way. Checked or not, it is a matter of the list or item container, not the object specified in it. Also, I don't want all my classes like user, role, group to have counterparts like checkableUser, checkableRole and checkableGroup.

The behavior I want can be easily complicated for the UI with

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

      

and a

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

      

But without binding to a checkbox, I cannot check if it is checked or not.

Is there a way to do something like this? An ideal solution for me would be to have listView1.SelectedItem, listView1.CheckedItems and possibly listView1.UncheckedItems and of course listView1.CheckItem and listView1.UncheckItem.

Thanks for any help.

+2


a source to share


2 answers


OK I understood. There wasn't much to do, but since I'm new to all WPF stuff, it was some work to figure out. Here's the solution:

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

      

Paste in your XAML under Window.Resources (clr = class namespace):

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

      



And this is how your CheckableListView:

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

      

Maybe it helps someone with the same problem.

+4


a source


To do this, you will need to create custom elements ListBox

and a custom ListBoxItem

to use in your application. Otherwise, you will have to add it to the items in your lists as a generic object ICheckable<T>

(where T is a user or role) and have it ICheckableCollection<ICheckable<T>>

for your items, rather than add model objects to check.



+1


a source







All Articles