WPF TabItem header 绑定不起作用
WPF TabItem header binding not working
我正在尝试绑定 TabItem
的 header 文本。
<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
s 将在 运行 时创建,因此以编程方式设置绑定:
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
}
也在 window 代码隐藏文件中:
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;
...
}
在 InitializeComponent();
之后设置 this.DataContext = this;
没有区别。
更改您的绑定:
binding.Source = this;
binding.Path = new PropertyPath("TabItemHeader_1");
我正在尝试绑定 TabItem
的 header 文本。
<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
s 将在 运行 时创建,因此以编程方式设置绑定:
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
}
也在 window 代码隐藏文件中:
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;
...
}
在 InitializeComponent();
之后设置 this.DataContext = this;
没有区别。
更改您的绑定:
binding.Source = this;
binding.Path = new PropertyPath("TabItemHeader_1");