gpt4 book ai didi

c# - ItemsPanel 模板无效

转载 作者:太空宇宙 更新时间:2023-11-03 14:17:11 26 4
gpt4 key购买 nike

我正在尝试将 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

我已经在应用程序的其他部分覆盖了 ItemsPanels,但出于某种原因,我没有看到这个

最佳答案

使用这个代替 ItemsTemplate

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

有效

关于c# - ItemsPanel 模板无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6321080/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com