WPF TabItem标头绑定不起作用

时间:2016-03-08 11:13:44

标签: c# wpf xaml binding tabitem

enter image description here

我试图绑定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();没有任何区别。

1 个答案:

答案 0 :(得分:1)

更改绑定:

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