1

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.

2
  • As you say this is a Model class. To track the UI changes you need a ViewModel class. Instead of binding your model directly, wrap it with a view model and bind to that class. Commented Apr 4, 2016 at 8:33
  • @qqww2, I know it should be wrapped in ViewModel but this way ViewModel get rather more complex. I am looking for simple solution instead. Can you please give direction using code specifically. Commented Apr 4, 2016 at 8:55

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.