2014-10-28 35 views
0

我想做一個非常簡單的事情(我認爲)與XAML。UserControls的ItemsControl與ViewModels

我想在一個'UserControl B'託管的ItemControl中顯示usercontrols'UserControl A'(with viewModels'ViewModel A')的列表。

'ViewModel B'包含一個屬性,它是'UserControls A'的observableCollection。

在「用戶控件A」的構造函數,我給自己定:

Me.DataContext = New ViewModelA() 
在「userControlB」的構造函數

,我已經設置相同,但用「視圖模型B」。 'ViewModel A'包含兩個屬性(整數)'Row'和'Col'。

我想要的是在'UserControl B'中託管的網格中綁定每個'userControl A'以及'Col'和'Row'的值。

這裏是我做了什麼: 用戶控件B:

<ItemsControl ItemsSource="{Binding ListOfUserControl_A}" Margin="1.5"> 
      <ItemsPanelTemplate> 
       <Grid> 
        <Grid.RowDefinitions> 
         <RowDefinition /> 
         <RowDefinition /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
         <ColumnDefinition /> 
        </Grid.ColumnDefinitions> 
       </Grid> 
      </ItemsPanelTemplate> 
      <ItemsControl.ItemContainerStyle> 
       <Style> 
        <Setter Property="Grid.Column" Value="{Binding Col}" /> 
        <Setter Property="Grid.Row" Value="{Binding Row}" /> 
       </Style> 
      </ItemsControl.ItemContainerStyle> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <local:UserControl_A/> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

收集這個過程之前,正確填寫。

但是在運行時我有這個錯誤(翻譯自法文): 「該集合必須爲空才能使用'ItemsSource'」。

的其他錯誤,我曾是「ItemTemplate中和ItemTemplateSelector不適用於UserControl_B」前...

我在做什麼錯?

回答

0

您忘記將ItemsPanelTemplate換成ItemsControl.ItemsPanel標記。如果沒有ItemsControl對待ItemsPanelTemplate作爲一個項目,因爲你已經設置ItemsSource你得到這個錯誤

<ItemsControl ItemsSource="{Binding ListOfUserControl_A}" Margin="1.5"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
</ItemsControl> 
+0

媽呀,這麼白癡。 Thx男人。 – Thib 2014-10-28 11:19:54

相關問題