Matteo Cultrera
Matteo Cultrera

Reputation: 60

C# XAML binding

I'm trying to implement an interface using XAML and C#. I created a XAML with a stack panel, and inisde this stack panel i have a text box with a minimum and maximum width and a line under it. I would like to change the lenght of the line based on the length of the textbox to have an underline of the textbox I'm writing (the textbox increases its width with the text inserted), but when I do this

<StackPanel Canvas.Left="410" Canvas.Top="111">
        <TextBox MinWidth="30" MaxWidth="193" HorizontalAlignment="Left" TextWrapping="NoWrap" BorderThickness="0" Background="#3C4149" FontSize="32" Foreground="White" Name="tb"/>
        <Line X1="0" X2="{Binding tb.Width, UpdateSourceTrigger=PropertyChanged}" StrokeThickness="4" Stroke="White" Fill="White" Height="10" HorizontalAlignment="Left"/>
</StackPanel>

The value of X2 doesn't change, and so the line remains without any length. How can I solve this?

Upvotes: 0

Views: 68

Answers (1)

Raviraj Palvankar
Raviraj Palvankar

Reputation: 879

You can use a Border control instead of line and assign the actual width of your TextBox using it's name:

<Border HorizontalAlignment="Left" BorderThickness="1" BorderBrush="Red" Width="{Binding ElementName=tb, Path=ActualWidth}"/>

Upvotes: 1

Related Questions