【发布时间】:2019-03-11 11:41:15
【问题描述】:
我的任务是绘制多个矩形并对其进行操作。所以我使用ItemsControl 和ItemsPanel 作为Canvas,并使用包装ScrollViewer:
<ScrollViewer Height="200" Grid.Row="1" >
<ItemsControl Name="rectanglesList" Background="AliceBlue">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Canvas.Left" Value="{Binding Y}"/>
<Setter Property="Canvas.Top" Value="{Binding X}"/>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black">
<Rectangle Width="{Binding Width}" Height="{Binding Height}" Fill="{Binding Color}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
现在由于 Canvas 的高度和宽度属性不包括它的孩子,Scrollviewer 将不起作用,除非我通过计算孩子的高度和宽度的总和并将它们分配给手动更改 ItemsControl 高度和宽度ItemsControl。
现在的问题是是否缺少任何自动执行此操作的属性?
【问题讨论】:
标签: c# wpf canvas itemscontrol