列表框项目的水平拉伸所需的ListBox模板调整

时间:2010-07-22 04:14:53

标签: silverlight xaml listbox

我正在尝试使用DataTemplate ListBox.ItemTemplate作为简单的TODO列表。

每个ListBoxItem的模板都是一个网格,我希望第二列的内容可以拉伸列表框的剩余宽度。没有任何数量的HorizontalAlignment="Stretch"等似乎可以做到这一点,我想我需要修改模板。我查看了ListBox提取的Xaml模板,但无法看到我需要更改的内容。

在这个XAML示例中,您可以看到一个绿色框,它应该拉伸listboxitem的剩余宽度,但不会。

在XamlPad / WPF中,此代码实际上按预期呈现。
在Silverlight中虽然盒子不会伸展。

 <ListBox Width="360" Height="150" HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                            <Grid  Margin="3,0,3,0" HorizontalAlignment="Stretch">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="URGENT" Foreground="Red" Margin="5,0,10,0" Grid.Column="0" VerticalAlignment="Center"/>
                                <Border BorderBrush="Green" BorderThickness="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,2">
                                    <TextBlock Margin="5,2" Text="{Binding}" FontWeight="Bold"  />
                                </Border>
                            </Grid>
                </DataTemplate>
         </ListBox.ItemTemplate>

            <s:String>Take out trash</s:String>
            <s:String>Clean car</s:String>
            <s:String>Finish TODO list program</s:String>
            <s:String>Sleep</s:String>

        </ListBox>

3 个答案:

答案 0 :(得分:0)

第一个ColumnDefinition定义为Auto,但未分配第一个TextBlock(URGENT)的Width属性。分配值或将ColumnDefinition.Width更改为固定值时会发生什么?

答案 1 :(得分:0)

您可以通过为ListBox定义ItemContainerStyle来实现您想要的效果:

    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
        <Setter Property="Padding" Value="3"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Background="{TemplateBinding Background}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                          <Grid  HorizontalAlignment="Stretch" Margin="3,0,3,0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="URGENT" Foreground="Red" Margin="5,0,10,0" Grid.Column="0" VerticalAlignment="Center"/>
                        <Border BorderBrush="Green" BorderThickness="1" Grid.Column="1" HorizontalAlignment="Stretch" Margin="0,2">
                            <TextBlock Margin="5,2" Text="{Binding}" FontWeight="Bold"  />
                        </Border>
                    </Grid>
                        <Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>

                        <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

然后在列表框中使用此样式,如下所示:

 <ListBox Width="360" Height="150" x:Name="lb" HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" />

在您尝试执行的操作中,问题是您的网格宽度不等于列表框项目。

答案 2 :(得分:0)

问题是DataTemplate中的Grid,如果你增加它的大小,你会开始看到边框随之增长。