StackPanel超过一定宽度时动态调整按钮的位置

Adjust the position of the button dynamically when StackPanel exceeds certain width

我需要调整一个按钮在网格中的位置,该网格在 srollviewer 中有一个堆栈面板,这样每当滚动条出现时它应该在末尾,当滚动条不可用时它应该是在最后一项旁边。

With Horizontal Scroll Bar ,正如预期的那样,但在 Without Horizontal scroll Bar 按钮和最后一个文本框之间有间隙。我怎样才能在不影响 [With Horizo​​ntal Scroll Bar] 行为的情况下消除这个差距。

这是使用的代码片段:

<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>

实现你想要的(粘性行为)。像这样在 XAML 中做一些更改。

<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>   

完全没问题。