2015-10-20 35 views
0

我有一個列表,與我想在組合框顯示的項目,但結果是,我沒有看到文本,但這樣的:如何FIL與MVVM光結合和XAML組合框?

App1.Data.Models.Test 
App1.Data.Models.Test 
App1.Data.Models.Test 

我真的不知道如何顯示適當的文本。

模型試驗有2個屬性ID和名稱。

<ComboBox Grid.Column="1" 
      Grid.Row="3" 
      Margin="10" 
      ItemsSource="{Binding TestList}" /> 

是否需要像ListView一樣使用DataTemplate?

回答

1

你需要從你想DisplayMemberPath顯示App1.Data.Models.Test定義屬性。 可選地重寫ToString()

<ComboBox Grid.Column="1" 
     Grid.Row="3" 
     Margin="10" 
     DisplayMemberPath = "Name" 
     ItemsSource="{Binding TestList}" /> 
+0

啊,是真的!用SelectedItem你會得到整個物體嗎? ID和姓名? – Bayern

+0

是的,你做。它綁定到類型App1.Data.Models.Test – Dom84

2

ComboBox使用其項的ToString()方法來顯示它們。

無論是覆蓋App1.Data.Models.Test.ToString(),或選擇App1.Data.Models.Test特定的屬性顯示,假設Name

<ComboBox 
     Grid.Column="1" 
     Grid.Row="3" 
     Margin="10" 
     ItemsSource="{Binding TestList}" 
     DisplayMemberPath="Name" /> 
+0

的財產報告說,財產「路徑」設置更多然後一次。 – Bayern