I need to adjust the position of a button inside a grid which has a stack panel inside a srollviewer, such a way that whenever the scroll bar appears it should be at the end and when ScrollBar is not available it should be next to the last item.
With Horizontal Scroll Bar , as expected but in Without Horizontal scroll Bar there is a gap between button and last textbox. How can I get rid of this gap without affecting the [With Horizontal Scroll Bar] behaviour.
Here is the code snippet used :
<Window x:Class="WpfApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp2"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled">
<StackPanel Orientation="Horizontal">
<TextBox Text="TextBox1" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox2" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox3" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox4" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox5" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
</StackPanel>
</ScrollViewer>
<Button Content="Button" Grid.Column="2"/>
</Grid>
</Window>
To achieve what you want (sticky behavior). Just make a few changes in XAML like this.
<Grid x:Name="ParentGrid">
<Grid Grid.Column="0" HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="0" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Disabled" HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<TextBox Text="TextBox1" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox2" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox3" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox4" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox5" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
<TextBox Text="TextBox6" VerticalContentAlignment="Center"></TextBox>
</StackPanel>
</ScrollViewer>
<Button Content="Button" Grid.Column="1" HorizontalAlignment="Left"/>
</Grid>
</Grid>
It works completely fine.