ListBox内容没有完全跨列表框延伸

时间:2009-09-11 09:03:06

标签: wpf listbox autosize

我正在尝试创建一个列表框,我可以完全控制列表框中每个项目的外观。

我可以让项目水平拉伸。但是,所选项目左侧有一层薄薄的蓝色条纹。 (在图片中,选择了中间项目。)

我可以让这条蓝色条带消失吗?

alt text http://img44.imageshack.us/img44/949/boundlistboxdisplay.jpg

这是完整的代码。

<Window x:Class="SimpleListTemplate.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     >
    <Window.Resources>
        <XmlDataProvider x:Key="DcCharacters">
            <x:XData>
                <Characters xmlns="">
                    <Character HeroName="Catwoman" Identity="Selina Kyle" />
                    <Character HeroName="Batman" Identity="Bruce Wayne" />
                    <Character HeroName="Starman" Identity="Jack Knight" />
                </Characters>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>
    <Grid>
        <ListBox 
             ItemsSource="{Binding Source={StaticResource DcCharacters}, XPath=//Characters/*}" 
             HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Label 
                        Content="{Binding XPath=@HeroName}" 
                        Height="40" 
                        VerticalContentAlignment="Center"
                        Background="LightGreen"
                        BorderThickness="2"
                        BorderBrush="DarkGreen"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:1)

以下是您的代码的更新。

<Window x:Class="SimpleListTemplate.Window1"     
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
 <Window.Resources>
    <XmlDataProvider x:Key="DcCharacters">
        <x:XData>
            <Characters xmlns="">
                <Character HeroName="Catwoman" Identity="Selina Kyle" />
                <Character HeroName="Batman" Identity="Bruce Wayne" />
                <Character HeroName="Starman" Identity="Jack Knight" />
            </Characters>
        </x:XData>
    </XmlDataProvider>
    <Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle">
        <Setter Property="Padding" Value="0,0,0,0"/>
    </Style>
</Window.Resources>
<Grid>
    <ListBox 
        ItemsSource="{Binding Source={StaticResource DcCharacters}, XPath=//Characters/*}"
        ItemContainerStyle="{StaticResource ContainerStyle}" 
        HorizontalContentAlignment="Stretch">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Label 
                    Content="{Binding XPath=@HeroName}"
                    Height="40"
                    VerticalContentAlignment="Center"
                    Background="LightGreen"
                    BorderThickness="2"
                    BorderBrush="DarkGreen"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
</Window>

这可以解决您的问题。