我有一个ListBox
,其中包含一个带有大量文本的TextBox
,应尽可能将其包装。 ListBox
与其他控件一起位于ScrollViewer
内部(在下面的示例中,它是Button
)。我希望ListBox
与ScrollViewer
一起水平伸展,并且仅在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
小于特定大小时显示水平条。
答案 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>
上的其他属性。