jeudi 25 janvier 2018

Get checked items in treeview

I'm using a third party .dll which contains a type of object called Layer. I have a collection of these layers in my viewmodel. In my view, I have a TreeView whose ItemsSource is bound to the collection of layers. I also have a checkbox with each item.

I want to somehow get the all of the checked Layer items.

Normally I would have just make a public boolean property called IsChecked in the object's class, but Layer does not have a property for that.

Here's the xaml:

<TreeView Grid.Row="1" Grid.Column="0">
    <TreeViewItem Header="Shape Files" ItemsSource="{Binding Layers}" >
        <TreeViewItem.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <CheckBox Content="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </TreeViewItem.ItemTemplate>
    </TreeViewItem>
</TreeView>

Here's the view model:

public ObservableCollection<Layer> Layers
{
    get { return mapModel.Layers; }
    set { mapModel.Layers = value; OnPropertyChanged("Layers"); }
}

And here's an example of what this looks like:

enter image description here

I know one way would be to have the checked function of the checkboxes bind to a command, with the item itself being sent as a command parameter. Is that really the best way to do this though?




Aucun commentaire:

Enregistrer un commentaire