ItemsPanel 模板没有效果

发布于 2024-11-14 23:04:30 字数 2862 浏览 3 评论 0原文

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

<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 声明为:

<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 视觉树: Snoop Visual Tree

我已经覆盖了应用程序其他部分中的 ItemsPanel,但由于某种原因,这个面板无法让我识别

I'm trying to swap the ItemsPanel in a ListBox for a WrapPanel but the ItemsPanelTemplate on the style doesn't seem to be having an effect. The style is found and applied because the border and background colours change, but inspecting with snoop shows no WrapPanel.

<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>

The ListBox is declared as:

<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 visual tree:
Snoop visual tree

I've overridden ItemsPanels in other parts of the app but for some reason this one is eluding me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滴情不沾 2024-11-21 23:04:30

使用它代替 ItemsTemplate

        <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="ListBox">
                 <Border Background="{TemplateBinding ListBox.Background}" CornerRadius="5">
                    <WrapPanel IsItemsHost="True"/>
                 </Border>
              </ControlTemplate>
           </Setter.Value>
        </Setter>

它有效

Use this instead of ItemsTemplate

        <Setter Property="Template">
           <Setter.Value>
              <ControlTemplate TargetType="ListBox">
                 <Border Background="{TemplateBinding ListBox.Background}" CornerRadius="5">
                    <WrapPanel IsItemsHost="True"/>
                 </Border>
              </ControlTemplate>
           </Setter.Value>
        </Setter>

It works

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文