2016-02-26 45 views
1

標題說這一切,但可以說我有一個接口IPersonIPerson具有UserControl作爲其屬性。如何將ItemsControl的ItemsSource綁定到IEnumerable內的類T中的屬性<T>?

在主XAML我已經有型IPerson的觀察集合的人,我已經綁定的ItemsSource與人民ItemsSource={Binding People}

我怎麼能顯示與IPerson ItemsControl中的相關聯的用戶控件?

接口

public interface IPerson 
{ 
    string DisplayName { get; } 

    /// <summary> 
    /// Call that is going to add the UI component to XML Editor 
    /// </summary> 
    UserControl DisplayingUserControl { get; } 
} 

在VM

public ObservableCollection<IPerson> People 
上查看

<ItemsControl ItemsSource="{Binding People}" Padding="3"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <XXX Content="{Binding DisplayingUserControl}" Height="Auto" Width="Auto"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

我想知道如何填寫XXX

+0

您是否想要在您的主窗口中綁定usercontrol? – Rohit

+0

編號MainWindow(技術上MainWindowViewModel,但)有'ObservableCollection ' –

+0

'在您的屬性您用於綁定,使用ObservableCollection 但請注意,對於數據網格,您將不得不將自動生成列設置爲YES,因爲列類型和名稱不是知道直到運行時間。 –

回答

1

比方說你有一個屬性UC在IPerson之間您可以顯示UserControl,如:

<ItemsControl x:Name="IC"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <Label Content="{Binding UC}" Height="Auto" Width="Auto"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
+0

非常感謝您的建議,但不幸的是,我無法顯示任何與此... –

+0

當我正在調試,即時消息看到加州大學爲空eventhough我雖然初始化它......嗯 –

+0

啊你的解決方案工作! –

相關問題