首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列表框项目水平拉伸所需的ListBox模板调整

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

Stack Overflow用户
提问于 2010-07-22 12:14:53
回答 3查看 847关注 0票数 0

我正在尝试使用ListBox.ItemTemplateDataTemplate来创建一个简单的待办事项列表。

每个ListBoxItem的模板是一个网格,我希望我的第二列的内容延伸到列表框的剩余宽度。没有数量的HorizontalAlignment="Stretch"等似乎做的把戏,我认为我需要修改模板。我已经查看了ListBox提取的Xaml模板,但是看不到需要修改的地方。

在这个XAML示例中,您可以看到一个绿色的框,它应该拉伸列表框项目的剩余宽度,但实际并非如此。

在XamlPad / WPF中,这段代码实际上可以按预期呈现。

然而,在Silverlight中,盒子不会伸展。

代码语言:javascript
复制
 <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>
EN

回答 3

Stack Overflow用户

发布于 2010-07-22 14:33:08

第一个ColumnDefinition被定义为自动,但第一个TextBlock (紧急)的宽度属性未分配。当您赋值或将ColumnDefinition.Width更改为固定值时会发生什么情况?

票数 0
EN

Stack Overflow用户

发布于 2010-07-22 16:05:19

您可以通过为ListBox定义一个ItemContainerStyle来实现您想要的功能:

代码语言:javascript
复制
    <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>

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

代码语言:javascript
复制
 <ListBox Width="360" Height="150" x:Name="lb" HorizontalContentAlignment="Stretch" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" />

在您尝试做的事情中,问题是您的网格宽度与列表框项目不相等。

票数 0
EN

Stack Overflow用户

发布于 2010-07-22 23:07:51

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3305737

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档