kuemme01
kuemme01

Reputation: 472

WrapPanel not wrapping when using it with ItemControl

I have a WrapPanel that I'm filling in my ViewModel with UserControls:

<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>

When I fill them manuell with random Controls, everything works fine! I already read something about problems because of Using ItemTemplate!? If its true, how can I manage that?

Thanks

Upvotes: 2

Views: 1197

Answers (1)

You're putting a single ItemsControl inside a WrapPanel. That won't do anything. If you want your ItemsControl to use a WrapPanel to host its own items, here's how:

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

    <!-- etc. -->

Note that something has to constrain the width of the ItemsControl for this to work: Either it must be limited by the size of its parent, or a Grid Column, or even by setting the Width or MaxWidth properties of the ItemsControl element itself, either directly or via a Style.

Upvotes: 6

Related Questions