I have a MainView with TabControl, which is intended to display child Views, and a ComboBox to display some values MainView has this XAML:
<TabControl ItemsSource="{Binding ViewModelsCollection, Mode=TwoWay}"
SelectedItem="{Binding ViewModelsSelectedItem, Mode=TwoWay}">
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock Text="{Binding Path=Title}"/>
</TextBlock>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
<ComboBox x:Name="cmbProfiles"
ItemsSource="{Binding Path=ProfileCollection}"
SelectedItem="{Binding Path=ProfileSelectedItem, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
DisplayMemberPath="Key">
</ComboBox>
Also, MainView has DataTemplates for child Views - to be used in the TabControl:
<Window.Resources>
<DataTemplate DataType="{x:Type vm:OrdersViewModel}">
<v:OrdersView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:ShippingViewModel}">
<v:ShippingView/>
</DataTemplate>
</Window.Resources>
ChildViews are created in MainViewModel:
ObservableCollection<ViewModelBase> collection = new ObservableCollection<ViewModelBase>();
collection.Add(new OrdersViewModel());
collection.Add(new ShippingViewModel());
this.ViewModelsCollection = collection;
Each ChildView has a button, which IsEnabled property should depend on MainView's cmbProfiles selection:
<Button IsEnabled="{Binding Path=BtnServiceSetupIsEnabled}"
Content="Next ...">
</Button>
Code in MainView to handle that:
IOrdersViewModel ordersViewModel = (IOrdersViewModel)this.ViewModelsCollection.Where(x => x.GetType().Equals(typeof(OrdersViewModel))).FirstOrDefault();
if (profileItem.Key == "Custom")
{
ordersViewModel.BtnServiceSetupIsEnabled = true;
}
else
{
ordersViewModel.BtnServiceSetupIsEnabled = false;
}
The problem occurs when I change selection in cmbProfiles: ChildViewModel.BtnServiceSetupIsEnabled gets updated in code indeed, but the ChildView doesn't reflect that change, button stays disabled. It does work fine if I don't use DataTemplates and simply add <v:OrdersView/> to my MainView, not dynamically filling the TabControl. How can I fix that - and to have my TabControl's TabItems created dynamically?