I am making a WPF application following MVVM. What I want in my application is that there is a View which contains some common buttons and text boxes and a TabControl
. TabControl will basically host different UserControls
. So for each UserControl
I have a separate View
and a ViewModel
ready.
So structure of my application looks like this.
MainWindow.Xaml
EntryView.Xaml
Button1
Button2
TabControl
UserControl1 (View)
UserControl2 (View)
UserControl3 (View)
Son in my EntryView I have the tab control. Now I need to bind this.
Here is what I have done.
EntryView.Xaml
<TabControl ItemsSource="{Binding Tabs}" SelectedItem="{Binding SelectedTab}">
<TabControl.ContentTemplate>
<DataTemplate DataType="{x:Type vm:UserControl1ViewModel}">
<v:UserControl1View/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:UserControl2ViewModel}">
<v:UserControl2View/>
</DataTemplate>
</TabControl.ContentTemplate>
<TabControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="Header"/>
</DataTemplate>
</TabControl.ItemTemplate>
</TabControl>
EntryViewModel.cs
private ObservableCollection<BaseViewModel> _tabs;
public ObservableCollection<BaseViewModel> Tabs
{
get
{
if (_tabs == null)
{
_tabs = new ObservableCollection<BaseViewModel>();
_tabs.Add(new UserControl1ViewModel());
_tabs.Add(new UserControl2ViewModel());
}
return _tabs;
}
}
But now when I run my application nothings happens. TabControl is empty. I put breakpoint inside Tabs in view model, but it didn't get hit. So question one is am I doing this right? If no then what should I do?