I am in a trouble with databinding of the tabControl's Content Template.
I have that class
public class MainWindowViewModel : INotifyPropertyChanged
{
string _text1 = "text1";
string _text2 = "text2";
string _text3 = "text3";
public string Text1
{
get
{
return _text1;
}
set
{
_text1 = value;
}
}
public string Text2
{
get
{
return _text2;
}
set
{
_text2 = value;
}
}
public string Text3
{
get
{
return _text3;
}
set
{
_text3 = value;
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
And I have that xaml :
<Window x:Class="LazyBindingTabControl.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LazyBindingTabControl"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl Height="299" Margin="0,12,0,0" Name="tabControl1" VerticalAlignment="Top">
<TabItem Header="tabItem1" Name="tabItem1">
<TabItem.ContentTemplate>
<DataTemplate>
<TextBlock Name="Text1" Text="{Binding Path=DataContext.Text1}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
<TabItem Header="tabItem2" Name="tabItem2">
<TabItem.ContentTemplate>
<DataTemplate>
<TextBlock Name="Text2" Text="{Binding Path=DataContext.Text2}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
<TabItem Header="tabItem3" Name="tabItem3">
<TabItem.ContentTemplate>
<DataTemplate>
<TextBlock Name="Text3" Text="{Binding Path=DataContext.Text3}" />
</DataTemplate>
</TabItem.ContentTemplate>
</TabItem>
</TabControl>
</Grid>
</Window>
And also I set dataContext in code-behinf of mainWindow.xaml
namespace LazyBindingTabControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}
But binding is not successful. How can I successfuly bind Text1, Text2 and Text3 properties properly.
Thanks.