Wpf 无法在自定义项控件上自动调整画布项spanel的大小:每个元素都使用MouseDragElementBehavior
我能够渲染一组项目,并用鼠标移动它们。我无法让canvas itemspanel自动调整大小。按原样尝试代码,查看矩形是如何渲染的,以及如何使用鼠标将其四处拖动。请注意,它们被约束到父边界(400x400)。接下来找到注释行Wpf 无法在自定义项控件上自动调整画布项spanel的大小:每个元素都使用MouseDragElementBehavior,wpf,wpf-controls,draggable,parent,itemscontrol,Wpf,Wpf Controls,Draggable,Parent,Itemscontrol,我能够渲染一组项目,并用鼠标移动它们。我无法让canvas itemspanel自动调整大小。按原样尝试代码,查看矩形是如何渲染的,以及如何使用鼠标将其四处拖动。请注意,它们被约束到父边界(400x400)。接下来找到注释行取消注释,并注释掉其上方的行。现在请注意矩形是如何正确渲染的,但一旦拖动一个矩形,它们就会飞到左上角,无法再移动!请帮忙 您将需要这些名称空间 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivit
取消注释,并注释掉其上方的行
。现在请注意矩形是如何正确渲染的,但一旦拖动一个矩形,它们就会飞到左上角,无法再移动!请帮忙
您将需要这些名称空间
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
下面是完整的XAML,请将其放到页面或控件中
<Grid x:Name="LayoutRoot">
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="ItemTemplateKey">
<Canvas Height="400" Width="400">
<!--<Canvas>-->
<Rectangle Height="50" Width="50" Fill="Red">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
</Rectangle>
</Canvas>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot2">
<ItemsControl
ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}"
ItemTemplate="{StaticResource ItemTemplateKey}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
</Grid>
编辑:
亲爱的耶稣!作为临睡前的最后一搏,我试着用网格替换画布,一切都成功了
以下是新的工作xaml,以防其他人有相同的问题:
<Grid x:Name="LayoutRoot">
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="ItemTemplateKey">
<Grid>
<Rectangle Height="50" Width="50" Fill="Red">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot2">
<ItemsControl
ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}"
ItemTemplate="{StaticResource ItemTemplateKey}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
</Grid>
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="ItemTemplateKey">
<Grid>
<Rectangle Height="50" Width="50" Fill="Red">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot2">
<ItemsControl
ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}"
ItemTemplate="{StaticResource ItemTemplateKey}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
我用这个答案编辑了上面的问题。把它放在这里回答/结束问题 以下是新的工作xaml,以防其他人有相同的问题:
<Grid x:Name="LayoutRoot">
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="ItemTemplateKey">
<Grid>
<Rectangle Height="50" Width="50" Fill="Red">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot2">
<ItemsControl
ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}"
ItemTemplate="{StaticResource ItemTemplateKey}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
</Grid>
<UserControl>
<UserControl.Resources>
<DataTemplate x:Key="ItemTemplateKey">
<Grid>
<Rectangle Height="50" Width="50" Fill="Red">
<i:Interaction.Behaviors>
<ei:MouseDragElementBehavior ConstrainToParentBounds="True"/>
</i:Interaction.Behaviors>
</Rectangle>
</Grid>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot2">
<ItemsControl
ItemsSource="{Binding anArrayOfThreeOrFourThingsInTheVM}"
ItemTemplate="{StaticResource ItemTemplateKey}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</UserControl>
…那么,结束这个问题的正确so协议是什么?我是否应该自己发布“答案”并将其标记为已回答?是的,请自己添加答案(如果允许),并将其标记为已回答。记住,有一天,有人会提出同样的问题,而你也会为他们回答!我就是那个人。谢谢你的回答!