【问题标题】:WPF ItemsPanelTemplate not workingWPF ItemsPanelTemplate 不起作用
【发布时间】:2010-12-22 09:47:55
【问题描述】:

我正在尝试让 ItemsPanelTemplateListBox 工作。 ListBoxDataTemplate 中使用,并且我的隐式 ListBox 样式都不会覆盖默认的视觉样式。出于某种原因,我为控件指定的ItemsPanelTemplateWrapPanel)被忽略,而使用StackPanel

这是整个模板,我只能假设我缺少一些导致此问题的东西。

<DataTemplate x:Key="GroupLargeIconsTemplate" DataType="{x:Type Core:IGroup}">
    <ListBox ItemsSource="{Binding Children}" OverridesDefaultStyle="True">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel HorizontalAlignment="Left" VerticalAlignment="Top" IsItemsHost="True"  ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto"
                           Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <DataTemplate.Resources>
                    <conv:IconConverter x:Key="IconConverter"/>
                </DataTemplate.Resources>
                <StackPanel Margin="2" Width="100" Height="140">
                    <Image Source="{Binding Icon,Converter={StaticResource IconConverter},ConverterParameter=96}"/>
                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</DataTemplate>

【问题讨论】:

  • 你好,虽然它的回复很长一段时间。你能告诉你是如何解决这个问题的吗?您说“控件模板覆盖了 ItemsPane”;有没有办法阻止这种情况发生?

标签: wpf data-binding listbox itemscontrol itemspanel


【解决方案1】:

我的猜测是 WrapPanel 中的 width 属性使其表现得像 Stackpanel。

【讨论】:

  • 我用 snoop 检查了它,该元素实际上是一个 StackPanel。
  • 在受挫之后,控件模板覆盖了 ItemsPanel。问题解决了!
【解决方案2】:

奇怪,控制模板不应该覆盖明确指定的值,而是相反的情况出现在我的脑海中...... 也就是说,除非控件模板提供了将“IsItemsHost”设置为 true 的面板。然后忽略 ItemsPanel。这可能是你的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-23
    • 2013-02-08
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多