Linking a groupbox to a textbox

39 Views Asked by At

I'd like to link the header of a groupbox to the content of a textbox. I need to use a user control that contains the textBox. I can't link this textBox to the header. Do you have a solution? My codes are underneath.

MainWindows :

<TabItem Header="_Travaux">
     <Grid>
         <Grid.ColumnDefinitions>
             <ColumnDefinition Width="*"/>
             <ColumnDefinition Width="*"/>
         </Grid.ColumnDefinitions>
         <GroupBox Grid.Column="0" Header="{Binding ElementName=UserControlLabel_Local, Path=Value}">
             <Grid Grid.IsSharedSizeScope="True">
                 <local:UserControl_Label x:Name="UserControlLabel_Local" LabelContent="Local"/>
             </Grid>
         </GroupBox>
      </Grid>
</TabItem>

UserControl :

  public partial class UserControl_Label : UserControl
  {
      private static readonly DependencyProperty LabelContentProperty = 
          DependencyProperty.Register("LabelContent", typeof(string), typeof(UserControl_Label));

      private static readonly DependencyProperty TextBoxValue =
          DependencyProperty.Register("Value", typeof(string), typeof(UserControl_Label));

      public string LabelContent
      {
          get { return (string)GetValue(LabelContentProperty); }
          set { SetValue(LabelContentProperty, value); }
      }

      public string Value
      {
          get { return TextBox_Value.Text; }
          set { SetValue(TextBoxValue, value); }
      }
      public UserControl_Label()
      {
          InitializeComponent();
          DataContext = this;
          DependencyPropertyDescriptor descriptorLabel = 
              DependencyPropertyDescriptor.FromProperty(LabelContentProperty, typeof(UserControl_Label));
          DependencyPropertyDescriptor descriptorTextBox =
              DependencyPropertyDescriptor.FromProperty(TextBoxValue, typeof(UserControl_Label));
          descriptorLabel.AddValueChanged(this, OnLabelContentChanged);
          descriptorTextBox.AddValueChanged(this, OnTextBoxValueChanged);
      }

      private void OnTextBoxValueChanged(object sender, EventArgs e)
      {
         TextBox_Value.Text = Value;
          Debug.WriteLine("Value changed");
      }
      private void OnLabelContentChanged(object sender, EventArgs e)
      {
          Label_Label.Content = LabelContent;
      }
  }
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="Label" Width="Auto"/>
        <ColumnDefinition SharedSizeGroup="DeuxPoints"  Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Label x:Name="Label_Label" Content="_Label" HorizontalAlignment="Right" VerticalAlignment="Center" Grid.Column="0" Grid.Row="0"/>
    <TextBlock x:Name="TextBlock_Value" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Grid.Column="1" Grid.Row="0" Text=":" Margin="5"/>
    <TextBox x:Name="TextBox_Value" TextWrapping="Wrap" VerticalAlignment="Center" Grid.Column="2" Grid.Row="0"/>
</Grid>

Thank you in advance for your reply.

I tried to change the path to TextBoxValue_Local. However, with a simple textBox, it works fine.

1

There are 1 best solutions below

0
gaetan worch On BEST ANSWER

I finally found the problem, I simply hadn't bind the textbox with the value.

 <TextBox x:Name="TextBox_Value" Text="{Binding Value}" TextWrapping="Wrap" VerticalAlignment="Center" Grid.Column="2" Grid.Row="0"/>