开发者

WPF Binding To Child Control

开发者 https://www.devze.com 2023-02-20 20:56 出处:网络
I\'ve got a TabControl which contains a nested ListView. The ListView is bound to the selected item in the pare开发者_JS百科nt TabControl. This works great in that switching tab displays the child ele

I've got a TabControl which contains a nested ListView. The ListView is bound to the selected item in the pare开发者_JS百科nt 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 :)


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消