I was looking for the answer on site but I didn't find it. I have a Windows template and within the template, I have a textbox.
<TextBox x:Name="WindowTextbox" Text="Type..." TextChanged="WindowTextbox_TextChanged"
FontSize="15" Foreground="White" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto"
AcceptsReturn="True" Background="#404040" BorderThickness="0" Grid.Row="0" Grid.Column="0"
Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="0 0 0 23">
</TextBox>
Also, I have another textbox that is created when I press the button that is dynamically created textbox.
void Button_Click_1(object sender, RoutedEventArgs e)
{
var window = new Window();
window.Show();
window.Width = 250;
window.Height = 250;
window.Template = FindResource("WindowControlTemplate1") as ControlTemplate;
TextBox dynamicTextBox = new TextBox();
Grid.SetRow(dynamicTextBox, 1);
Grid.SetColumn(dynamicTextBox, 0);
this.TextPanel.Children.Add(dynamicTextBox);
}
So what I want to do is that when I write something in Textbox that is within the template at the same time this text should appear in the dynamically created textbox
CodePudding user response:
If I understand it correctly.
Instead of this
TextBox dynamicTextBox = new TextBox();
make private field
private TextBox _dynamicTextBox;
void Button_Click_1(object sender, RoutedEventArgs e)
{
...
...
_dynamicTextBox = new TextBox();
Grid.SetRow(dynamicTextBox, 1);
Grid.SetColumn(dynamicTextBox, 0);
this.TextPanel.Children.Add(_dynamicTextBox);
}
In your TextChanged
event of WindowTextbox
set the text to your _dynamicTextBox
.
private void WindowTextbox_TextChanged(object sender, TextChangedEventArgs e)
{
if(sender is TextBox txt)
{
_dynamicTextBox.Text = txt.Text;
}
}