2017-07-14 33 views
0

爲什麼我的ItemsControl不是爲每個項目創建一個ContentPresenter?我猜這是什麼讓我的項目不出現(當我使用Live Visual Tree進行檢查時,它們被設置爲可見和正確的位置)。我基本上重複使用不同的ItemsControl上面的代碼,並且在搜索Google/Stackoverflow時遇到了這個問題,我一直無法找到任何東西。我可以包含視圖模型代碼,但我不認爲它是相關的,因爲我在Live Property Explorer中看到了適當的值,並且可以看到每個WellContainer都在它的合適的網格單元格中。ItemsControl未創建ContentPresenter

XAML:

<ItemsControl 
    Grid.Row="1" 
    Grid.Column="1" 
    ItemsSource="{Binding Wells}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Grid 
       x:Name="m_WellGrid" 
       Margin="5" 
       wpf:GridHelpers.RowCount="{Binding RowCount}" 
       wpf:GridHelpers.ColumnCount="{Binding ColumnCount}"> 
      </Grid> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style> 
      <Setter 
       Property="Grid.Row" 
       Value="{Binding Path=WellRow}"/> 
      <Setter 
       Property="Grid.Column" 
       Value="{Binding Path=WellCol}"/> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <TextBlock 
       Text="A" 
       Margin="4"/> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

實況視覺樹檢查:

enter image description here

+0

WellContainer定義在哪裏?你是否明確定義了一個'ContentPresenter'? –

+0

我的視圖模型有一個'WellContainer'的'ObservableCollection','WellContainer'只是繼承自'Control'的同一個項目中的一個類。不,我沒有明確定義'ContentPresenter'只是'DataTemplate',因爲這是我在另外兩個似乎在附近工作的情況下所做的。 – m4gik

+0

如果你的內容存在並且像你說的那樣處於正確的位置,請檢查'Z-Index'並確保它不僅僅隱藏在 –

回答

1

ItemsControl設計只在必要時包裹在容器中的項目,那就是,當項目不有資格成爲自己的容器。從您的評論我們發現WellContainer派生自Control,因此有資格作爲自己的容器並沒有包裹在ContentPresenter。不幸的是,沒有辦法直接控制這種行爲,但是您可以子類ItemsControl並覆蓋ItemsControl.IsItemItsOwnContainerOverride方法來修改默認行爲。


正如我們在ItemsControlsource code看看它是足夠的項目是UIElement類型的,纔有資格將自己的容器。

+2

)以外,每個屬性都很好看視圖模型中的控件集合是一個糟糕的主意,您將無法以多於一個ItemsControl,因爲一個UI元素只能有一個父元素,即不是多個父元素的子元素。 – Clemens

+0

好的很有道理,謝謝。我會試着想出一個解決方案並更新問題。 – m4gik