2010-08-18 49 views
1

我遇到了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

的代碼behinf
namespace LazyBindingTabControl 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      this.DataContext = new MainWindowViewModel(); 
     } 
    } 
} 

但結合設置的DataContext不成功。我怎樣才能成功地綁定Text1,Text2和Text3屬性。

謝謝。

回答

2

綁定的TabItem內容以文本1,文本2,文本3屬性

然後TextBlock中的一部分,結合這樣

Text={Binding} 

文本,使代碼會是這樣

<TabItem Header="tabItem1" Name="tabItem1" Content="{Binding Text1}"> 
     <TabItem.ContentTemplate> 
     <DataTemplate> 
      <TextBlock Name="Text1" Text="{Binding}" /> 
     </DataTemplate> 
     </TabItem.ContentTemplate> 
    </TabItem> 

應該工作

0

我改變了你的XAML和視圖模型到它應該是什麼樣的工作

視圖模型: 當屬性設置NotifyPropertyChanged必須被調用。我加了屬性的1個例,與所有3

public string Text1 
    { 
     get 
     { 
      return _text1; 
     } 
     set 
     { 
      _text1 = value; 
      NotifyPropertyChanged("Text1"); 
     } 
    } 

XAML做到這一點:

更改數據綁定:

Text="{Binding Path=Text1}" 

這些改變它應該工作之後。