如何在ScrollViewer中制作一个具有TextBox拉伸的WPF ListBox?

时间:2019-01-11 23:27:27

标签: wpf xaml listbox 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>

问题在于,无论ListBox的宽度如何,TextBox(以及ScrollViewer)都将达到其最大宽度。水平条总是显示出来(当然,除非ScrollViewer的宽度超过ListBox的宽度)。我希望仅在ListBox小于特定大小时显示水平条。

2 个答案:

答案 0 :(得分:0)

我不知道您为什么需要列表框,我认为您可以使用它

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

答案 1 :(得分:0)

经过关于StackOverflow的反复试验和其他回答之后,我得出了以下解决方案:

<h1 style="text-align: center; background-color: #273b86; color: #ffffff; padding: 5px; border-radius: 5px; width: 640px; margin: auto;">GROW YOUR BUSINESS WITH US</h1>

唯一的更改是<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> 上的其他属性。