如何在 ScrollViewer 中制作带有 TextBox 拉伸的 WPF ListBox?

How do I make a WPF ListBox with a TextBox stretch inside a ScrollViewer?

我有一个 ListBox,其中包含一个带有大量文本的 TextBox,应尽可能将其换行。 ListBox 与其他控件一起位于 ScrollViewer 内(在下面的示例中,它是 Button)。我希望 ListBoxScrollViewer 一起水平拉伸,只有当 ListBox 小于一定宽度时才显示水平滚动条。这是我得到的 XAML:

<StackPanel>
    <ScrollViewer
        VerticalScrollBarVisibility="Auto"
        HorizontalScrollBarVisibility="Auto"
        >
        <StackPanel>
            <Button Content="Test" />
            <ListBox MinWidth="500">
                <ListBoxItem>
                    <TextBox
                        Text="This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test."
                        TextWrapping="Wrap"
                        />
                </ListBoxItem>
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</StackPanel>

问题在于,无论 ScrollViewer 的宽度如何,ListBox(因此 TextBox)都会达到其最大宽度。水平条始终显示(当然,除非 ScrollViewer 的宽度超过 ListBox 的宽度)。我希望水平条仅在 ListBox 小于特定尺寸时显示。

我不知道你为什么需要 ListBox 我认为你可以使用它

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Button Content="Test" />
    <TextBox Text="This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test.This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a" 
             TextWrapping="Wrap"
             Grid.Row="1"
             VerticalScrollBarVisibility="Auto"/>
</Grid>

在 Whosebug 上反复试验和其他答案之后,我得出了以下解决方案:

<StackPanel>
    <ScrollViewer
        VerticalScrollBarVisibility="Auto"
        HorizontalScrollBarVisibility="Auto"
        >
        <StackPanel>
            <Button Content="Test" />
            <ListBox
                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                HorizontalContentAlignment="Stretch"
                Width="{Binding ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"
                MinWidth="500"
                >
                <ListBoxItem>
                    <Grid>
                        <TextBox
                            Text="This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test."
                            TextWrapping="Wrap"
                            />
                    </Grid>
                </ListBoxItem>
            </ListBox>
        </StackPanel>
    </ScrollViewer>
</StackPanel>

唯一的变化是 ListBox 上的附加属性。