【问题标题】:ItemsPanel Template has no effectItemsPanel 模板无效
【发布时间】:2011-09-13 08:18:36
【问题描述】:

我正在尝试将 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 可视化树:

我已经在应用程序的其他部分覆盖了 ItemsPanels,但由于某种原因,我忽略了这个

【问题讨论】:

    标签: c# .net wpf itemspanel itemspaneltemplate


    【解决方案1】:

    用这个代替 ItemsTemplate

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

    有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-21
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多