【问题标题】:Silverlight WrapPanel not displaying items consistentlySilverlight WrapPanel 没有一致地显示项目
【发布时间】:2011-01-25 22:30:07
【问题描述】:

我们希望使用 WrapPanel 来显示不同数量的按钮(实际上是行为类似于按钮的用户控件)。每个 WrapPanel 内部都有一个 ItemsControl 及其项目。 WrapPanel 通常不会显示所有项目——如果有四个,你只会看到一两个。行为不一致。

我们做错了什么吗?使用这样的 WrapPanel 是否有任何已知问题?

对于 XAML,这是我们主窗口中的 UserControl:

<UserControl x:Name="ucCatalogContent" Grid.Row="2">
   <local:Catalog_CategoryView />

这是 CategoryView 标记。这有 ItemsControl。它的项目是其他带有 WrapPanel 的用户控件:

<UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
   xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
   xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
   xmlns:local="clr-namespace:Catalog;assembly=Catalog"   
   x:Class="Catalog.Catalog_CategoryView"
   >

   <UserControl.Resources>

          <DataTemplate x:Key="CategoryDT" >
                 <local:Category />
          </DataTemplate>

   </UserControl.Resources>

   <ScrollViewer x:Name="scvCatalogCategoryView" 
          HorizontalScrollBarVisibility="Disabled">

          <!-- This is the item that should be bound to the collection of categories -->
          <ItemsControl x:Name="icCategories" 
                 ItemTemplate="{StaticResource CategoryDT}"
          >

          <local:Category x:Name="item1" />
          <local:Category x:Name="item2" />
          <local:Category x:Name="item3" />

          </ItemsControl>

   </ScrollViewer>

这是使用 WrapPanel 的单个类别:

<UserControl
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     
   xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
   xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" 
   xmlns:custom="clr-namespace:CustomControlResources;assembly=CustomControlResources"
   xmlns:local="clr-namespace:Catalog;assembly=Catalog"   
   x:Class="Catalog.Category"
   >

   <UserControl.Resources>

          <ItemsPanelTemplate x:Key="CategoryItemPanel">                             
                 <toolkit:WrapPanel 
                        VerticalAlignment="Top"
                        HorizontalAlignment="Stretch"
                        />                                                                                 
          </ItemsPanelTemplate>


          <DataTemplate x:Key="OfferingDT" >
                 <local:OfferingTile x:Name="offeringTile" />
          </DataTemplate>

   </UserControl.Resources>

   <Grid x:Name="LayoutRoot" Style="{StaticResource ContentRootStyle}">

          <Grid.RowDefinitions>
                 <RowDefinition Height="Auto" />
                 <RowDefinition Height="*" />                    
          </Grid.RowDefinitions>


          <custom:BlockExpander x:Name="expCategoryExpander"
                 Title="access [bind me]">

                 <custom:BlockExpander.BlockExpanderContent>

                       <ItemsControl x:Name="icServiceOfferingsList"
                              ItemsPanel="{StaticResource CategoryItemPanel}" 
                              ItemTemplate="{StaticResource OfferingDT}"
                              >
                              <local:OfferingTile />
                              <local:OfferingTile />
                              <local:OfferingTile />
                              <local:OfferingTile />

                       </ItemsControl>                          

                 </custom:BlockExpander.BlockExpanderContent>

          </custom:BlockExpander>

   </Grid>

在此屏幕截图中,每个扩展器标题(蓝色三角形)应该有一个标题,每个组应该包含四个项目:

【问题讨论】:

  • 我认为这是因为您将控件包裹在 ScrollViewer 中(加上水平滚动条被禁用)。滚动查看器的范围是无限的,因此需要知道父级宽度的项目可能无法正常工作。
  • 这似乎不太可能。有时所有四个在所有 3 行中都显示正常,有时您在任何一行中都没有显示,或者在一个中显示两个,在另一个中显示三个。扩展器标题也不显示。

标签: silverlight user-controls itemscontrol toolkit wrappanel


【解决方案1】:

事实证明,扩展器 UserControl 和 WrapPanel 之间存在一些交互。一旦我们删除了代码隐藏,包装面板就会正常运行。

我们通过设置 Toolkit 扩展器的样式来解决这个问题。我们以前没有使用工具包,但因为我们需要包装板......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多