1

I cant quite figure out how to bind my list view to both properties in my ViewModel and a list in my view model.

Is it possible to override the ListViews ItemsSource and bind a Checkbox within the ListView straight to the parent ViewModel?

Something like this...

        <ListView ItemsSource="{Binding Path=SomeListInViewModel}">
            <ListView.View>
                <GridView>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <CheckBox 
                                        IsEnabled="{Binding SomePropertyInViewModel}" 
                                        IsChecked="{Binding SomePropertyInsideListObj, Mode=TwoWay}" />
                                </DockPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
1

1 Answer 1

3

there are two possible ways:

1) name the ListView

<ListView x:Name="MyListView" ItemsSource="{Binding Path=SomeListInViewModel}">

and then use the ElementName property on the binding

IsEnabled="{Binding ElementName=MyListView, Path=Whatever}" 

2) use RelativeSource in the binding

IsEnabled="{Binding Path=Whatever, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" 

in both cases, if your view model is set as the DataContext of the ListView, the path would look something like DataContext.SomePropertyInViewModel.

Sign up to request clarification or add additional context in comments.

5 Comments

Not sure that I follow you... I can get the list obj to bind to my view OK using the ItemsSource, what I cannot do is set the IsEnabled property to a value on my VM as it goes straight to the list property. The VM is the data context of the window, I'm using the MVVMLight framework.
What you need to be aware of is the "Source" part of a binding. ItemsControls set the DataContext property of each item, and, in the absence of any other source specification, bindings use DataContext as their source. What I've outlined above are two different ways of specifying the source. What can I clarify for you?
also, the end of this how-to has a pretty good description of the different options for specifying a binding source. msdn.microsoft.com/en-us/library/ms746695.aspx
Thanks, this helped. As I am using MVVMLight the Locator is used as the VM's dataContext I Just had to do something like: IsEnabled="{Binding ViewModel.Property, Source={StaticResource Locator}}"
@baileyswalk Thanks for the tip. Your comment helped me fix my issue too.

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.