【问题标题】:WrapPanel not wrapping when using it with ItemControlWrapPanel 与 ItemsControl 一起使用时不换行
【发布时间】:2018-05-14 21:09:55
【问题描述】:

我有一个 WrapPanel,我正在用 UserControls 填充我的 ViewModel:

<WrapPanel Width="250" Orientation="Horizontal" Margin="3">
   <ItemsControl ItemsSource="{Binding PlaceableObjectsContent}">
      <ItemsControl.ItemTemplate>
          <DataTemplate DataType="{x:Type local:PlaceableObjectViewModel}">
              <local:PlaceableObjectUserControl>
                 <local:PlaceableObjectUserControl.InputBindings>
                     <MouseBinding MouseAction="LeftClick"
                                                  Command="{Binding DataContext.OnPlaceableObjectClicked, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
                                                  CommandParameter="{Binding}"/>
                 </local:PlaceableObjectUserControl.InputBindings>
              </local:PlaceableObjectUserControl>
          </DataTemplate>
      </ItemsControl.ItemTemplate>
   </ItemsControl>
</WrapPanel>

当我用随机控件填充它们时,一切正常!由于使用 ItemTemplate,我已经阅读了一些关于问题的内容!? 如果是真的,我该如何处理?

谢谢

【问题讨论】:

    标签: c# wpf itemscontrol itemtemplate wrappanel


    【解决方案1】:

    您将单个 ItemsControl 放入 WrapPanel。那不会有任何作用。如果您希望 ItemsControl 使用 WrapPanel 来托管自己的项目,方法如下:

    <ItemsControl 
        ItemsSource="{Binding PlaceableObjectsContent}"
        Width="250"
        Margin="3"
        >
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    
        <!-- etc. -->
    

    请注意,必须限制 ItemsControl 的宽度才能使其工作:它必须受到其父级或网格列的大小的限制,或者甚至通过设置 ItemsControl 元素本身的 Width 或 MaxWidth 属性,直接或通过样式。

    【讨论】:

    • 你这个人!谢谢!现在我明白了,我的问题很尴尬!哈哈
    • 请注意,您需要为 ItemsControl 定义一个宽度才能使其正常工作。
    • @gusmallysupportsMonica 当然,ItemsControl 需要限制其宽度。我不喜欢对 WPF 初学者说“定义宽度”,因为这听起来像是“您必须设置 Width 属性”。我已经编辑了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多