2013-05-11 101 views
0

我在將ObservableCollection綁定到ComboBoxRadComboBox)時遇到了一些問題。我以前使用過幾乎相同的代碼,但現在由於某種原因,它不起作用(ComboBox中沒有任何顯示)。將自定義對象的ObservableCollection綁定到ComboBox

這是我的ComboBox代碼:

<telerik:RadComboBox ItemTemplate="{StaticResource ComboBoxServerPropertiesTemplate}" DataContext="{Binding libpm:Collection.Properties}" ItemsSource="{Binding Name}"/> 

DataTemplate看起來是這樣的:

<DataTemplate x:Key="ComboBoxServerPropertiesTemplate"> 
    <Grid Margin="0 3"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition /> 
     </Grid.ColumnDefinitions> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 
     <TextBlock Grid.ColumnSpan="2" Text="{Binding Name}" /> 
     <TextBlock Grid.Row="1" Text="Value:" /> 
     <TextBlock Grid.Row="1" Foreground="#FFA2D0FF" Margin="40,0,0,0" Text="{Binding Value}" /> 
    </Grid> 
</DataTemplate> 

Collection()看起來是這樣的:

public static class Collection 
{ 
    public static ObservableCollection<ServerProperties.Property> Properties { get; set; } 
} 

而且ServerPropeties.Property看起來像這樣:

public sealed class Property 
{ 
    public string Name { get; set; } 
    public string Value { get; set; }  
} 

我嘗試使用"{Binding Path=libpm:Collection.Properties}"兩個DataContextItemSource,我也試圖把"{Binding libpm:Collection.Properties}"ItemSource,但它不工作。

任何幫助,將不勝感激。

回答

3

你似乎在ItemsSource已經設置爲一個字符串屬性不是一個集合

嘗試將ItemsSource你收集和使用DisplayMemberPathComboBox

<telerik:RadComboBox ItemTemplate="{StaticResource ComboBoxServerPropertiesTemplate}" ItemsSource="{Binding libpm:Collection.Properties}" DisplayMemberPath="Name"/> 
+0

拋出異常顯示Name財產:'不能同時設置DisplayMemberPath和ItemTemplate.' – davidwroxy 2013-05-11 14:04:29

+0

對,如果您使用的是ItemTemplate,則不能使用DisplayMemberPath。選擇一個或另一個。 – Xcalibur37 2013-05-11 14:23:41

+0

我也試過,但它不起作用。 – davidwroxy 2013-05-11 14:26:22

相關問題