C# WrapPanel与ItemControl一起使用时未进行包装
我有一个WrapPanel,我正在用UserControls填充我的ViewModel:C# WrapPanel与ItemControl一起使用时未进行包装,c#,wpf,itemscontrol,itemtemplate,wrappanel,C#,Wpf,Itemscontrol,Itemtemplate,Wrappanel,我有一个WrapPanel,我正在用UserControls填充我的ViewModel: <WrapPanel Width="250" Orientation="Horizontal" Margin="3"> <ItemsControl ItemsSource="{Binding PlaceableObjectsContent}"> <ItemsControl.ItemTemplate> <DataTemplate
<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时出现问题的文章!?
如果这是真的,我该怎么办
谢谢您正在将单个Items控件放入包装袋中。那没用。如果您希望ItemsControl使用WrapPanel来承载自己的项目,下面是如何实现的:
<ItemsControl
ItemsSource="{Binding PlaceableObjectsContent}"
Width="250"
Margin="3"
>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- etc. -->
请注意,必须约束ItemsControl的宽度才能使其正常工作:它必须受其父项或网格列大小的限制,甚至可以直接或通过样式设置ItemsControl元素本身的宽度或MaxWidth属性 你就是那个人!非常感谢。现在我明白了,我的问题很尴尬!HahaNote您需要为ItemsControl定义一个宽度才能使其工作。@gusmallysupportsMonica毫无疑问,ItemsControl需要限制其宽度。我不喜欢对WPF初学者说“定义宽度”,因为这听起来像是“你必须设置
width
属性”。我已经编辑了答案。