我正在創建一個拖放啓用的應用程序,我一直在嘗試使用ItemsControl進行綁定,而不是默認添加我的控件。 Displayer是一個UserControl,代表我正在使用的對象。Siverlight 4,MVVM和ItemsControl ZIndex
ZExtender具有ZIndex,Top,Left等屬性。下面的實現正確地適用於頂部,左側,寬度,但ZIndex更改不起作用。我想要綁定或更改代碼中的ZIndex以在UI中自動更改它。這可能沒有使用消息傳遞到我的頁面後面的代碼?
<Border BorderBrush="Black" BorderThickness="2" Margin="35" HorizontalAlignment="Center" VerticalAlignment="Center">
<Canvas AutomationProperties.AutomationId="designerCanvas" Width="{Binding Root.Width}" Height="{Binding Root.Height}">
<Canvas.Background>
<ImageBrush ImageSource="/SilverlightImages/canvasBackground.png" Stretch="None" />
</Canvas.Background>
<ItemsControl ItemsSource="{Binding Containers}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<canvas> <!-- canvas is needed for the positioning to work -->
<controls:Displayer DataContext="{Binding ZExtender}"
Canvas.Top="{Binding Top}"
Canvas.Left="{Binding Left}"
Canvas.ZIndex="{Binding ZIndex}"
Width="{Binding WidthInPixels}"
Height="{Binding HeightInPixels}"
/>
</canvas>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Canvas>
</Border>
所以我需要重寫我的Displayer控件中的PrepareContainerForItemOverride?我是否需要基於我的DataContext(它是一個ViewModel)以編程方式設置綁定? – Aligned 2011-06-09 18:54:35
@Kevin - 你需要創建一個自定義ItemsControl並覆蓋它的PrepareContainerForItemOverride。您也可以重寫GetContainerForItem並將您的Displayer實例作爲優化返回到ContentPresenter的位置。 – CodeNaked 2011-06-09 18:56:44
感謝您的見解。我決定使用消息傳遞(而不是過度)來添加和刪除Canvas的Children屬性中的控件。我將頂部,左側,寬度,高度和ZIndex的綁定移到了控件中,這似乎是一個很好的方法。 Silverlight 5會支持setter綁定嗎? – Aligned 2011-06-09 20:07:07