2015-01-08 28 views
0

我有以下XAML代碼,橢圓移入和移出矩形區域。我想通過繪製一個矩形來標記這個區域,但是我找不到在畫布中繪製這個區域的方法,因爲無法將這個矩形添加到我的observableCollection「氣球」中。向ItemsControl添加多個形狀,不在集合中

<ItemsControl Name="IC" ItemsSource="{Binding balloons}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <Canvas Name="canvasWorld" Background="Blue" Width="{Binding canvasWidth}" Height="{Binding canvasHeight}" IsItemsHost="True"/> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemContainerStyle> 
       <Style TargetType="ContentPresenter"> 
        <Setter Property="Canvas.Left" Value="{Binding XPos}"/> 
        <Setter Property="Canvas.Top" Value="{Binding YPos}"/> 
       </Style> 
      </ItemsControl.ItemContainerStyle> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 

        <Ellipse Width="{Binding Width}" Height="{Binding Height}" Fill="{Binding color}" Stroke="{Binding colorBorder}" StrokeThickness="3"> 
         <Ellipse.RenderTransform> 
          <TranslateTransform X="-20" Y="-20"/> 
         </Ellipse.RenderTransform> 
        </Ellipse> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

我試過hierichial datatemplate,但我不能讓它工作。理想情況下,我想要數據綁定矩形的大小和位置,但現在用靜態值繪製它將是一個很好的開始。

+0

你想讓每個氣球移出一個專門的矩形,還是你想讓所有的氣球共享同一個矩形? –

+0

我想所有的氣球共享一個矩形。基本上,retangle只是爲了可視化。根據風力數據,我有氣球四處移動,然後我想計算一下氣球在畫布上矩形區域覆蓋的面積,所以我需要的是說明矩形的位置。 – TBH

回答

0

我找不到在itemscontrol中添加它的方法,但是我通過在逼真的旁邊添加一個畫布,然後在畫布上添加Canvas.Left設置爲負值的圖形來實現我想要的效果值。