我想将组框的标题链接到文本框的内容。我需要使用包含文本框的用户控件。我无法将此文本框链接到标题。 你有解决方案吗? 我的代码在下面。
主窗口:
<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>
用户控制:
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>
提前感谢您的回复。
我尝试将路径更改为
TextBoxValue_Local
。
然而,使用一个简单的文本框,它就可以正常工作。
我终于找到问题了,我只是没有将文本框与值绑定。
<TextBox x:Name="TextBox_Value" Text="{Binding Value}" TextWrapping="Wrap" VerticalAlignment="Center" Grid.Column="2" Grid.Row="0"/>