Hello @BitSmithy ,
Welcome to Microsoft Q&A!
It is recommended that you use element binding Text="{Binding ElementName=rectangleTest, Path=Width}"
, and change the Rectangle.WidthProperty
in event TextChanged
.
<StackPanel>
<Rectangle x:Name="rectangleTest" Height="200" Width="100" Fill="Red"/>
<TextBox x:Name="textboxTest" Text="{Binding ElementName=rectangleTest, Path=Width}" TextChanged="textboxTest_TextChanged" Height="50" Width="100"/>
<Button Content="change the width" Click="Button_Click" />
</StackPanel>
private void textboxTest_TextChanged(object sender, TextChangedEventArgs e)
{
double dbWidth;
if (double.TryParse(textboxTest.Text, out dbWidth))
{
if (dbWidth >= 0)
{
rectangleTest.Width = dbWidth;
}
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
rectangleTest.Width = 300;
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.