Furqan Safdar
Furqan Safdar

Reputation: 16698

WPF ListView multiselection using custom CheckBox inside

I have a fairly complex object model having nested custom collection objects/models as shown below:

public sealed class LibraryInfo : NamedModel
{       
    public ClassInfos _classes;       
    public ClassInfos Classes
    {
        get { return _classes; }
        set { SetProperty(ref _classes, value); }
    }
}

public class ClassInfos : List<ClassInfo> { }

public sealed class ClassInfo : NamedModel
{
    public PropertyInfos _properties;     
    public PropertyInfos Properties
    {
        get { return _properties; }
        set { SetProperty(ref _properties, value); }
    }
}

public class PropertyInfos : List<PropertyInfo> { }

public sealed class PropertyInfo : NamedModel
{
}

I want to bind LibraryInfo classes on one ListView for classes selection.

<ListView SelectedItem="{Binding SelectedClass}" ItemsSource="{Binding LibraryInfo.Classes}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        </GridView>
    </ListView.View>
</ListView>

And based on ClassInfo selection I want to show properties of selected class on another ListView for properties selection.

<ListView ItemsSource="{Binding SelectedClass.Properties}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
        </GridView>
    </ListView.View>        
</ListView>

The problem is that how can i track checked (CheckBox IsChecked) items because my original Model do not contain any such property like IsActive, to keep UI related field decoupled from my Model.

I am looking for an elegant and simple solution to this problem.

Upvotes: 1

Views: 367

Answers (1)

AnjumSKhan
AnjumSKhan

Reputation: 9827

Use Blend behaviors.

Use a Collection in your ViewModel for selected items.

Write an ICommand called UpdateSelectedItemsCommand and pass 1/0 parameters for adding/deleting items.

        <CheckBox ...>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Checked">
                    <i:InvokeCommandAction Command="{Binding UpdateSelectedItemsCommand}"
                                                            CommandParameter="1"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="Unchecked">
                    <i:InvokeCommandAction Command="{Binding UpdateSelectedItemsCommand}"
                                                            CommandParameter="0"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </CheckBox>

Upvotes: 1

Related Questions