baileyswalk
baileyswalk

Reputation: 1228

Data Binding or DataContext issue when binding a ListView from multiple sources

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>

Upvotes: 1

Views: 4582

Answers (1)

dtm
dtm

Reputation: 792

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.

Upvotes: 3

Related Questions