2016-03-08 103 views
1

enter image description hereWPF TabItem的頭綁定不工作

我想一個TabItem的標題文字綁定。

<StackPanel> 
    <TabControl Name="tabControl"> 
     <TabItem Name="tabItem_1" 
       Header="--- Tab A ---" /> 
     <TabItem Name="tabItem_2" 
       Header="--- Tab B ---" /> 
    </TabControl> 
    <Button Name="btAction" 
      Content=" _Action " 
      HorizontalAlignment="Left" 
      Margin="0,20,0,0" 
      Click="btAction_Click" /> 
</StackPanel> 

由於TabItem旨意在運行時創建,綁定設置程序:

private void Window_ContentRendered(object sender, EventArgs e) 
{ 
    // Create a TextBlock for the TabItem header 
    TextBlock textBlock = new TextBlock(); 
    textBlock.Text = "FirstHeaderText"; 

    // Create a TabItem, make the TextBlock the header and add the TabItem to the TabControl 
    TabItem tabNew = new TabItem(); 
    tabNew.Header = textBlock; 
    tabControl.Items.Add(tabNew); 

    // Create and set binding 
    Binding binding = new Binding(); 
    binding.Mode = BindingMode.OneWay; 
    binding.NotifyOnSourceUpdated = true; 
    binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 
    binding.Source = this.TabItemHeader_1; 
    // If this line is omitted "FirstHeaderText" is shown, otherwise string.Empty: 
    textBlock.SetBinding(TextBlock.TextProperty, binding); 
} 

private void btAction_Click(object sender, RoutedEventArgs e) 
{ 
    string currentValue = this.TabItemHeader_1; 
    this.TabItemHeader_1 = "NewHeaderText"; 
    // Result of last line: header text remains unchanged 
} 

此外,在隱藏文件窗口代碼:

using System.ComponentModel; 
using System.Runtime.CompilerServices; 
... 

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    #region --- INotifyPropertyChanged-Implementation A -------------------------------------------------------------------- 

    // Without "= delegate { }" PropertyChanged stays null 
    public event PropertyChangedEventHandler PropertyChanged = delegate { }; 

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    #endregion --- INotifyPropertyChanged-Implementation Z ----------------------------------------------------------------- 

    #region --- binding property A ----------------------------------------------------------------------------------------- 

    public string TabItemHeader_1 
    { 
     get 
     { 
      return this.tabItemHeader_1; 
     } 

     set 
     { 
      if (value != this.tabItemHeader_1) 
      { 
       this.tabItemHeader_1 = value; 
       NotifyPropertyChanged(); 
      } 
     } 
    } 
    #endregion --- binding property Z -------------------------------------------------------------------------------------- 

    private string tabItemHeader_1; 

    ... 
} 

設置this.DataContext = this;InitializeComponent();後不做區別。

+0

對不起,我以前的答案,看到新的一個。 – Giangregorio

回答

1

更改綁定:

binding.Source = this; 
binding.Path = new PropertyPath("TabItemHeader_1");