2009-08-12 93 views
65

我想數據綁定到這個ItemsControl設置畫布的屬性在一個ItemsControl DataTemplate中

<ItemsControl ItemsSource="{Binding Path=Nodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 

通過使用此DataTemplate,我想單獨正確地定位在CanvasNode元素:

<DataTemplate DataType="{x:Type Model:EndNode}"> 
    <Controls:EndNodeControl Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}" /> 
</DataTemplate> 

但是,它沒有按預期工作。我所有的節點元素都在同一位置上彼此重疊。有關如何完成此任務的任何建議?

回答

111

附加屬性僅適用於畫布的直接子項。 ItemsControl中將會把ContentPresenter控件的直接孩子,所以你可能要添加一個樣式爲以及:

<ItemsControl ItemsSource="{Binding Path=Nodes}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="Canvas.Left" Value="{Binding Path=XPos}" /> 
      <Setter Property="Canvas.Top" Value="{Binding Path=YPos}" /> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
</ItemsControl> 

希望這有助於

+0

謝謝。我在5分鐘前自己找到了這個解決方案。我想我發佈這個問題有點快。 :) – atsjoo 2009-08-12 10:43:04

+7

呵呵..我也喜歡那些AHA時刻;)..而且它並不全是壞的..但是有一天你的問題也許能幫助別人..你永遠不會知道! – Arcturus 2009-08-12 10:45:28

+0

它確實,謝謝 – amaca 2009-08-24 16:34:32

相關問題