我想把一个组框的标题链接到一个文本框的内容。我需要使用一个包含文本框的用户控件。我不能把这个文本框链接到标题。你有解决方案吗?我的代码在下面。
主窗口:
<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,它工作得很好。
1条答案
按热度按时间ncgqoxb01#
我终于找到了问题,我只是没有绑定文本框的值。
字符串