Reputation: 435
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Text="Name:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="5"/>
<TextBox Grid.Column="1" x:Name="txtName" Text="{Binding InName, Mode=TwoWay}" Margin="0 5" Width="200"/>
<TextBlock Text="Type:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,4,10,6" Grid.Row="1"/>
<ComboBox Grid.Column="1" x:Name="cmbInsuredType" Text="{Binding InType, Mode=OneWayToSource}" Margin="35,4,165,0" Width="200" Grid.ColumnSpan="3" Grid.Row="1"/>
<TextBlock Grid.Row="1" Text="Active:" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="0,36,11,-26"/>
<CheckBox Grid.Row="1" x:Name="chkIsActive" Grid.Column="1" IsChecked="{Binding Active, Mode=OneWayToSource}" VerticalAlignment="Center" Grid.ColumnSpan="2" Margin="35,39,45,-26" />
</Grid>
i want name xaml textbox control to be resized(grow/shrink) according to the entered user input horizontally and not vertically.pleas help
Upvotes: 1
Views: 1694
Reputation: 21
Remove the width on your textbox and set HorizontalAlignment="Stretch" on the textbox
Upvotes: 1
Reputation: 1510
You can start from this code:
<TextBox Height="20" TextChanged="TextBox_TextChanged" MinWidth="50" Padding="0" />
And in your code behind
private Size MeasureString(string candidate, TextBox tb)
{
var formattedText = new FormattedText(
candidate, CultureInfo.CurrentUICulture, FlowDirection.LeftToRight, new Typeface(tb.FontFamily, tb.FontStyle, tb.FontWeight, tb.FontStretch), tb.FontSize, Brushes.Black);
return new Size(formattedText.Width, formattedText.Height);
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is TextBox)
((TextBox)sender).Width = MeasureString(((TextBox)sender).Text, (TextBox)sender).Width;
}
Upvotes: 0