首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ItemsPanel模板不起作用

ItemsPanel模板不起作用
EN

Stack Overflow用户
提问于 2011-06-12 17:00:36
回答 1查看 543关注 0票数 2

我正在尝试将ListBox中的ItemsPanel替换为WrapPanel,但是样式上的ItemsPanelTemplate似乎没有效果。找到并应用该样式是因为边框和背景颜色发生了变化,但使用snoop进行检查时没有显示WrapPanel。

代码语言:javascript
复制
<Style x:Key="CocktailGrid" TargetType="ListBox" BasedOn="{StaticResource {x:Type ListBox}}">
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Background" Value="White" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="Black" />
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="SelectionMode" Value="Single" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
    <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel
                     IsItemsHost="True"
                     Width="{Binding
                              Path=ActualWidth,
                              RelativeSource={RelativeSource
                                  Mode=FindAncestor,
                                  AncestorType=
                                     {x:Type ScrollContentPresenter}}}" />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80" />
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Image Grid.Row="0" Source="{Binding ImageName}" Height="80" Stretch="Uniform"/>
                    <TextBlock Grid.Row="1" Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
                </Grid>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ListBox声明为:

代码语言:javascript
复制
<ListBox x:Name="lstCocktails" PreviewKeyDown="dg_PreviewKeyDown" ItemsSource="{Binding Source={StaticResource drinksSource}}" SelectedItem="{Binding SelectedItem,ElementName=root,Mode=TwoWay}" Style="{StaticResource CocktailGrid}"
             SelectionMode="Single" MouseDoubleClick="lstCocktails_MouseDoubleClick">

Snoop可视化树:

我已经在应用程序的其他部分重写了ItemsPanels,但出于某种原因,这一部分无法实现

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-12 18:02:27

使用它而不是ItemsTemplate

代码语言:javascript
复制
        <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="ListBox">
                 <Border Background="{TemplateBinding ListBox.Background}" CornerRadius="5">
                    <WrapPanel IsItemsHost="True"/>
                 </Border>
              </ControlTemplate>
           </Setter.Value>
        </Setter>

它起作用了

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

https://stackoverflow.com/questions/6321080

复制
相关文章

相似问题

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