我遇到了tabControl的內容模板數據綁定問題。DataBinding問題在TabControl的ContentTemplate中
我有類
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));
}
}
}
而且我有一個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>
同時,我在mainWindow.xaml
的代碼behinfnamespace LazyBindingTabControl
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}
但結合設置的DataContext不成功。我怎樣才能成功地綁定Text1,Text2和Text3屬性。
謝謝。