2011-06-14 113 views
0

我懷疑我的問題的答案很簡單,但我在論壇上看了一眼,似乎找不到任何相同內容。ItemsControl只顯示第一項

我有一個相當複雜的綁定結構,在最遠端有一個ItemsControl,但它只顯示第一個項目。我可以看到代碼方面的數據是正確的,並且只保存8個項目,第一個項目正確顯示,並具有所需的所有標籤和顏色。

邏輯端結構是這樣的:

  • 主窗口偏類
    • WaterSamplerClass的的ObservableCollection
      • ParametersClass(一類的單個實例)bottleStateClass
          • 的ObservableCollection int Number(屬性)
          • 字符串標籤(屬性)

的XAML結構是這樣的:

  • 主窗口偏類
    • ControlTe mplate分組框中使用bottleState控制模板包含按鈕
      • DataTemplate中(從代碼設置列表的DataContext WaterSampler實例)
      • 的ItemsControl使用的DataTemplate來顯示大量bottleStates的使用
    • 包含按鈕的裝飾的ControlTemplate ItemsControl中的

下面的代碼的簡化版本:

其中datacontext是WaterSampler實例的GroupBox ControlTemplate中的Items控件及其數據模板。

<ControlTemplate x:Key="WaterSamplerGroupBoxTemplate" TargetType="{x:Type GroupBox}" > 
    <GroupBox Header="{Binding Path=Header}" Width="300" Margin="10,5,10,0" HorizontalAlignment="Center"> 
    <StackPanel Orientation="Horizontal"> 
     <StackPanel.Resources> 
     <DataTemplate x:Key="BottleStateDataTemplate"> 
       <Button Template="{DynamicResource ValveStatusTemplate}" /> 
     </DataTemplate> 
     </StackPanel.Resources> 
    <ItemsControl Name="bottleStateListBox" ItemTemplate="{StaticResource BottleStateDataTemplate}" Margin="5" Height="50" ItemsSource="{Binding BottleIsFullList}" DataContext="{Binding Parameters}"/> 
    </StackPanel> 
    </GroupBox> 
</ControlTemplate> 

簡化按鈕控制模板:

<ControlTemplate x:Key="ValveStatusTemplate" TargetType="{x:Type Button}" > 
     <StackPanel Orientation="Vertical" Width="30" Margin="5" >   
      <TextBlock Text="{Binding Number}" FontSize="18" Canvas.Left="8"/> 
     </StackPanel> 
</ControlTemplate> 

保持該項目模板數據的類:

public class DisplayBottleStateClass : INotifyPropertyChanged 
{ 
    private int number; 
    public int Number 
    { 
     get { return number; } 
    } 
} 

含有DisplayBottleClass的列表中的類:

public class WSParametersClass : INotifyPropertyChanged 
{ 
    private List<DisplayBottleStateClass> bottleIsFullList = new List<DisplayBottleStateClass>(); 

    public List<DisplayBottleStateClass> BottleIsFullList 
    { 
     get { return bottleIsFullList; } 
    } 
} 

含參數類的類:

public class WaterSampler : INotifyPropertyChanged 
{ 
    private WSParametersClass parameters = new WSParametersClass(); 

    public WSParametersClass Parameters 
    { 
     get { return parameters; } 
     set { parameters = value; OnPropertyChanged("Parameters"); } 
    } 
} 

最後MainWindow類:

public partial class MainWindow : Window 
{ 
    public class WaterSamplerListClass : ObservableCollection<WaterSampler> { } 
    private WaterSamplerListClass waterSamplers = new WaterSamplerListClass(); 
    public MainWindow() 
    { 
     waterSamplers.Add(new WaterSampler(0)); 
     WaterSampler0Group.DataContext = (waterSamplers[0]); 
    } 
} 

我得到兩個刷子,我使用,但有約束力的錯誤,當我在XAML固定值替換這些仍然只顯示第一個元素。該列表似乎也可以通過綁定正確更新,因爲我可以看到正確更改的第一個元素。我得到的錯誤是:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=GradientStopHighlight; DataItem=null; target element is 'GradientStop' (HashCode=23577486); target property is 'Color' (type 'Color')

任何建議將是非常受歡迎的。

非常感謝

埃德

+0

這是一個很大的運動部件,並且您未示出在路徑和元素錯誤。如果你簡化問題,你可能會有更多的運氣來獲得答案。將代碼和xaml刪除到最低限度。 – 2011-06-14 18:26:51

+0

這是我現在面臨的問題中最接近的問題。我徘徊你解決了嗎?我沒有任何綁定錯誤,但仍然只有嵌套列表中的第一個元素顯示在視圖中。謝謝。 – ljubomir 2012-12-27 12:35:13

回答

0

你的ItemsControl聲明既是一個的ItemsSource和DataContext的。我相信這兩個屬性是相互排斥的 - 設置DataContext屬性會將控件與邏輯控制樹關聯的數據斷開。

刪除DataContext =數據綁定,我想你會看到ItemsSource中的項目出現在你的ItemsControl中。

另外,請注意ItemsControl本身不提供任何UI定義。出於調試的目的,放入ListBox或其他具體的ItemsControl併爲其提供相同的數據綁定設置以驗證您的數據綁定是否正常是非常有用的。如果ItemsControl沒有顯示任何東西,但是ListBox沒有顯示,那麼你的問題不在數據綁定中,而是在模板化ItemsControl的視覺效果。

0

嘗試在您的ItemsControlItemsPanelTemplate.ItemsStackPanel上設置CacheLength屬性。

CacheLengthMSDN定義:

The size of the buffers for items outside the viewport, in multiples of the viewport size. The default is 4.0.

Remarks

To improve scrolling performance, ItemsStackPanel creates and caches item containers for items that are off-screen on both sides of the viewport. The CacheLength property specifies the size of the buffers for the off-screen items. You specify CacheLength in multiples of the current viewport size. For example, if the CacheLength is 4.0, 2 viewports worth of items are buffered on each side of the viewport. You can set a smaller cache length to optimize startup time, or set a larger cache size to optimize scrolling performance. Item containers that are off-screen are created at a lower priority than those in the viewport.