Windows Phone 7.1項目(XAML)。 我有一個網格作爲模板的itemscontrol,綁定到數據元素的集合,一切正常。 但是,我必須添加一個額外的圖像到網格,它不綁定到集合。某種標題圖片。以Grid作爲模板的ItemsControl:將控件添加到Grid
我有這樣的代碼:
<ItemsControl ...>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True" x:Name="ShipsGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
<ColumnDefinition Width="0.1*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
<RowDefinition Height="0.1*"></RowDefinition>
</Grid.RowDefinitions>
<!--<Image Source="/images/image.png" x:Name="SuperImage"/>-->
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Image x:Name="ElementImage" Source="{Binding ImageUri, Mode=OneWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</controls:ShipItemsGridControl>
如果我取消的形象ItemsPanelTemplate(X:名稱SuperImage),我收到以下錯誤:無法明確修改面板的Children集合作爲ItemsPanel爲ItemsControl的。 ItemsControl爲Panel生成子元素。
我已經嘗試了幾件事,但我無法設法讓它工作。當然,我可以將它添加到itemtemplate,並且只能在第一項中顯示,但這非常非常難看。
感謝您的回覆。父容器不是一個選項,因爲圖像必須位於網格中定義的10列/行中。我如何將它添加到代碼隱藏中?我應該綁定哪個事件?不理想,但實際上我認爲最後的選擇。 – Boland 2013-03-13 14:49:42
@Boland根據你父類容器的類型,你可以堆疊你的對象,使圖像位於你的ItemsControl之上。 「Grid」和「Canvas」都將允許子對象彼此堆疊在一起。默認情況下,添加的最後一個子項位於頂部,但您也可以使用Canvas.ZIndex來操縱哪個對象位於頂部。如果我確實走了另一條路線,我只需使用'Loaded'事件將一些代碼附加到'ItemContainerGenerator'上,該代碼將在完成所有項目的生成後添加最終項目。 – Rachel 2013-03-13 14:59:25
如果我這樣做(因此ItemsControl之外的圖像),我必須重新聲明所有行和列的定義,對吧?我有10行和10列,所以這將是很多額外的XAML。 – Boland 2013-03-13 15:11:58