lundi 29 juin 2015

WPF- Bind tab selected/unselected event to method in tab viewmodel

I've got a ViewModel that holds a List of ViewModels that will be open as tabs by the view, and I want methods within the tab viewmodels to run when a tab gets selected/unselected. Currently I've got the TabControl in the view set up liek this:

<Window.Resources>
    <ResourceDictionary>
        <DataTemplate DataType="{x:Type VM1}">
            <View1 />
        </DataTemplate>
        <DataTemplate DataType="{x:Type VM2}">
            <View2 />
        </DataTemplate>
        <DataTemplate DataType="{x:Type VM3}">
            <View3 />
        </DataTemplate>

        <DataTemplate x:Key="TabItemTemplate">
            <Grid Width="Auto" Height="Auto">
                <ContentPresenter ContentSource="Header" Margin="3" 
                        Content="{Binding NiceName}"
                        HorizontalAlignment="Center" VerticalAlignment="Center">
                </ContentPresenter>
            </Grid>
        </DataTemplate>
    </ResourceDictionary>
</Window.Resources>

<TabControl Grid.IsSharedSizeScope="True" 
                SelectedIndex="{Binding CurrentTab, Mode=TwoWay}" 
                ItemsSource="{Binding Tabs}" 
                ItemTemplate="{StaticResource TabItemTemplate}" />

This all works fine, but I can't figure how to bind a tab change event to a methos within the tabbed viewmodels. You can change TabControl.SelectionChanged, but this would force the tabcontrol holding ViewModel to know what to call on it's tabs, forcing them to implement a specific interface. Is there any way to avoid this coupling in the tab holder ViewModel?

Aucun commentaire:

Enregistrer un commentaire