CatBusStop
CatBusStop

Reputation: 3428

WPF Binding To Child Control

I've got a TabControl which contains a nested ListView. The ListView is bound to the selected item in the parent TabControl. This works great in that switching tab displays the child elements in the ListView. What I can't figure out, is how to bind to the ListView's SelectedItem from outside of the Menu UserControl.

i.e.

<TabControl x:Name="Parent">
    <TabControl.ContentTemplate>
        <DataTemplate>
            <ListView x:Name="Child" 
                      ItemsSource="{Binding Path=SelectedItem.Tabs, ElementName=Parent}"/>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>

<ItemsControl ItemsSource="{Binding Path=SelectedItem.Controls, ElementName=Child}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            ... controls go here ...
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

I'm using M-V-VM so don't want to do my binding in code ideally - I'm sure it's possible, just can't figure it out :)

Upvotes: 1

Views: 3031

Answers (1)

Emond
Emond

Reputation: 50672

In general, if you need a property on a higher level you could move the property to the ViewModel that is bound to the higher level.

So, if I understand correctly, I would move the property of the ViewModel that is bound to the SelectedItem to the VM of the TabControl.

Does this make sense?

Upvotes: 1

Related Questions