2015-05-21 19 views
2

我有一個ComboBox轉換項目使用一種方法來標記

<ComboBox x:Name="SearchGendersComboBox" Grid.Row="3" Grid.Column="1" 
          IsEditable="True" 
          SelectedValuePath="Key" 
          DisplayMemberPath="Value" 
          SelectedValue="{Binding Path=GenderId}" 

          /> 

,我這個填充它採購組合框:

public void BindComboBoxes() 
{    
    SearchGendersComboBox.ItemsSource = new BindingSource(GenderMgr.GetGendersDropDown(true), null);    
    SearchGendersComboBox.SelectedIndex = 0; 
} 

這是GenderMgr

public class GenderMgr 
{ 

    public static Dictionary<byte, string> GetGendersDropDown(bool isFilterMode = false) 
    { 
     return GenderDb.RetrieveGendersDropDown(isFilterMode); 
    } 

} 

如何使用我的GenderMgr.GetGendersDropDown填寫ComboBox m arkup?

+0

您不會在WPF應用程序中使用'System.Windows.Forms.BindingSource'。從這裏開始閱讀:[數據綁定概述](https://msdn.microsoft.com/en-us/library/ms752347.aspx),特別是*綁定到集合*章節。 – Clemens

回答

1

丟掉BindingSourceItemsSource應該直接爲GenderMgr.GetGendersDropDown(true),即字典。其他一切都很好。

至於轉換爲標記:在某些情況下,您需要從某處獲取對象,因此除了可能使用ObjectDataProvider來調用XAML中相應的數據提供方法外,您無法做的很多事情。通常你會有一些屬性來保存視圖模型中的項目,ItemsSource然後仍然可以綁定在XAML中;只要視圖模型支持更改通知,該屬性就可以在任何時候分配數據。

相關問題