我按照此代码创建了可调整大小/可移动的画布。我想将ItemsControl
添加到Canvas
:
<ItemsControl ItemsSource="{Binding Units}"
Grid.Column="1"
Grid.Row="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left"
Value="{Binding X}" />
<Setter Property="Canvas.Top"
Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentControl Canvas.Top="{Binding Y}"
Canvas.Left="{Binding X}"
Width="{Binding Width}"
Height="{Binding Height}"
Template="{StaticResource DesignerItemTemplate}">
<Rectangle IsHitTestVisible="False"
Fill="Black" />
</ContentControl>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但是当我调整项目大小或移动项目时,Canvas.Left
和Canvas.Top
的值不会改变,我认为因为它们的值受ContentPresenter
限制,而不是ContentControl
。
我该如何解决这个问题?谢谢!