0
votes

I have two textboxes, the first of them I've bound to a tabControl's Item's Header. I did it in the code behind.

The problem is that the binding succeeds only in the first time.

XAML:

  <StackPanel>
    <TabControl x:Name="tabcontrol">
        <TabControl.Items>
            <TabItem>
            </TabItem>
        </TabControl.Items>
    </TabControl>
    <TextBox Text="5" x:Name="txtbox"></TextBox>
    <TextBox Text="dsfdf"></TextBox>
</StackPanel>

Code Behind:

 public partial class Window4 : Window 
 {
      public Window4() 
      {
         InitializeComponent();
         var b = new Binding { Mode = BindingMode.OneWay, Source = txtbox.Text };
         ((TabItem)tabcontrol.Items[0]).SetBinding(HeaderedContentControl.HeaderProperty, b);
      }
   }
1

1 Answers

0
votes

Change the Source to the TextBox, and set the Path to Text:

var b = new Binding { Mode = BindingMode.OneWay, Source = txtbox, Path = "Text" };
((TabItem)tabcontrol.Items[0]).SetBinding(HeaderedContentControl.HeaderProperty, b);

Otherwise, you've set the source to the current value of txtbox.Text, and any changes to it won't be picked up because the source (the string) does not support change notifications.