2009-10-28 74 views
5

我目前正在爲下面用一個ItemsControl對象的集合綁定到畫布在Silverlight 3綁定一個長方形的位置:的Silverlight 3 - 數據畫布

<ItemsControl x:Name="ctrl" ItemsSource="{Binding myObjectsCollection}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas></Canvas> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Rectangle Stroke="LightGray" Fill="Black" StrokeThickness="2" 
        RadiusX="15" RadiusY="15" Canvas.Left="{Binding XAxis}" 
        Height="25" Width="25"> 
      </Rectangle> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

不幸的是它似乎在結合Canvas.Left被忽略。從我學到的here中可以看出,這是由於項目放置在內容展示器中,而不是我在項目面板中指定的實際畫布。

有沒有一種方法可以使用數據綁定來確定畫布上元素的位置?

回答

7

我意識到,這已經接受了答案,但要實現最初的目標,而不帶空白搞亂的方法是創建一個自定義的ItemsControl並重寫PrepareContainerForItemOverride方法。在這種方法中,您可以在代碼中設置綁定。

public class CustomItemsCollection 
    : ItemsControl 
{ 
    protected override void PrepareContainerForItemOverride(DependencyObject element, object item) 
    { 

     FrameworkElement contentitem = element as FrameworkElement; 
     Binding leftBinding = new Binding("Left"); // "Left" is the property path that you want to bind the value to. 
     contentitem.SetBinding(Canvas.LeftProperty, leftBinding); 

     base.PrepareContainerForItemOverride(element, item); 
    } 

} 
1

你說得對,在Canvas和Rectangle之間插入一個ContentPresenter。 一種解決方法是設置左邊距,而不是Canvas.Left

<Rectangle Stroke="LightGray" Fill="Black" StrokeThickness="2" 
     RadiusX="15" RadiusY="15" Height="25" Width="25"> 
    <Rectangle.Margin> 
     <Thickness Left="{Binding XAxis}"/> 
    </Rectangle.Margin> 
</Rectangle> 
+0

不幸的是,屬性Left是隻讀的,無法設置。 – Blounty 2009-10-29 13:32:35

+1

我不知道它爲什麼是隻讀的原因。 無論如何,我設法使用值轉換器: 但這當然不是一個優雅的解決方案。 – Mart 2009-10-29 16:21:18

+0

LeftMarginConverter如何工作以及何時應用這個邊界,是相對於容器左邊緣應用於所有邊界項目的邊距? – Blounty 2009-10-30 10:55:34

-2

以下添加到您的ItemsControl

<ItemsControl.ItemContainerStyle> 
    <Style TargetType="{x:Type ContentPresenter}"> 
     <Setter Property="Canvas.Left" Value="{Binding XPath=XAxis}"/> 
    </Style> 
    </ItemsControl.ItemContainerStyle> 

無需任何自定義控制

+1

這不能工作,因爲ItemsControl在Silverlight(v3或v4)中沒有ItemContainerStyle屬性。在WPF是的,但這不是問題。 – Anthony 2010-07-11 22:36:49

2

你不能在Silverlight中使用ItemsControl.ItemContainerStyle。它不存在。它只存在於幾個葉級別類,如ListBox本身。

1

我知道這個問題有點舊,但你可以使用渲染變換 - 我正在做類似的事情;

<ItemsControl ItemsSource="{Binding Notes}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas Background="Aqua"/> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Border Width="{Binding W}" Height="{Binding H}" BorderBrush="Navy" BorderThickness="5" CornerRadius="10"> 
       <TextBlock Text="{Binding Text}"/> 
       <Border.RenderTransform> 
        <TransformGroup> 
         <RotateTransform Angle="0"/> 
         <TranslateTransform X="{Binding X}" Y="{Binding Y}"/> 
        </TransformGroup> 
       </Border.RenderTransform> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl>