在ListBox的DataTemplate中设置Horizo​​ntalAligment =“Stretch”时出现问题

时间:2011-04-06 18:14:56

标签: c# wpf xaml

我希望listBox中的项目水平拉伸,但如果项目的内容大于列表框,则会出现水平滚动条。怎么避免这个?

的Xaml:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="testTemplate">
            <Border x:Name="border" 
                    BorderBrush="Black"
                    BorderThickness="1" 
                    Margin="2" 
                    Padding="2" 
                    HorizontalAlignment="Stretch">
                <TextBlock Text="{Binding}" />
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox x:Name="listBox" 
                 VerticalAlignment="Stretch" 
                 HorizontalAlignment="Stretch" 
                 HorizontalContentAlignment="Stretch" 
                 ItemTemplate="{StaticResource testTemplate}" />
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:2)

ScrollViewer.HorizontalScrollBarVisibility设为Disabled

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    ...
</ListBox>