Step:I put a WrapPanel into the ScrollViewer. There is a button 1 in the window. Click button 1 and add a Button to the WrapPanel. When the number of Buttons in the WrapPanel exceeds the boundary of the WrapPanel, a vertical scroll bar will appear.
My question:I encountered a problem with a vertical scroll bar: when the vertical scroll bar appeared, the WrapPanel (or ScrollViewer?) changed to increase the width of a scroll bar (equivalent to a scroll bar outside). This problem will Causes the layout of other nearby controls to change. How can I fix it?
Window XAML:
Title="MainWindow" Height="450" Width="800">
<Grid>
<Border x:Name="Boder1" BorderThickness="1" BorderBrush="Black" Margin="0,0,368,0">
<ScrollViewer x:Name="scrList" VerticalScrollBarVisibility="Auto" Margin="0,0,-2,-2">
<WrapPanel x:Name="wplContent" Width="{Binding Width, ElementName=scrList, Mode=OneWay}">
<Button x:Name="CreateButton" Content="Button" Height="50" Width="50" Click="CreateButton_Click"/>
</WrapPanel>
</ScrollViewer>
</Border>
<Label Content="-Ranging" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="397,0,0,0"/>
</Grid>
</Window>
CS:
private void CreateButton_Click(object sender, RoutedEventArgs e)
{
Button CButton = new Button();
CButton.Width = 50;
CButton.Height = 50;
CButton.Content = "Button";
wplContent.Children.Add(CButton);
}
Click CreateButton under 65. When the number of button rows exceeds WrapPanel, you will find that the button moves to the left due to the appearance of the scroll bar. This is my problem.
Picture1:
https://i.loli.net/2021/09/10/YbZFWnst2V4ofGM.png[
^]
Picture2:
https://i.loli.net/2021/09/10/DyxTAsaXVUCr19c.png[
^]
What I have tried:
I found an example:
Give a WPF Grid the width of a ScrollViewer minus the scrollbar - Stack Overflow[
^]
But it has nothing to do with my problem.