2012-04-24 10 views
2

私のコンボボックスアイテムの名前と値を私のリストから取得したいのですが、私はビューモデルにコンボボックスアイテムリストを保持させたくありません。WPF/C# - バインディングリスト<string> to combobox

私は、リストA、B、C、D

public List<String> ComboList { get; set; } 

...

ComboList = new List<String>(); 
ComboList.Add("A"); 
ComboList.Add("B"); 
ComboList.Add("C"); 
ComboList.Add("D"); 

と私のコンボボックス

<ComboBox Margin="29,40,0,526" Width="212" Height="35" Grid.Row="1" ItemsSource="{Binding Path=ComboList, Mode=OneTime}" SelectedValuePath="Key" DisplayMemberPath="Value"></ComboBox>

を得たが、それは私に空を与えますコンボボックス...

enter image description here

+1

どこからキー/値を取得していますか?これは辞書であり、リストではないのですか? – scottheckel

+0

オハイオ州のたわごと、thatsの問題 – David

+0

hehe、ええ、それは常に最も頭痛を引き起こす明らかなものです。 – scottheckel

答えて

4

SelectedValuePathDisplayMemberPath属性を削除します。 これは一例です。彼らは間違ってる。

1

あなただけのコードビハインドにInitializeComponents前にそれを行うことを忘れ:

public void MainWindow(){ 
    this.Datacontext = this; 
    InitializeComponent() 
} 

はまた、あなたが直接リストをバインドすることはできません、あなたがより良いのObservableCollectionを与える必要があります。

public ObservableCollection<NetworkCard> NetworksCards { get { return m_aCards; } } 

private ObservableCollection<NetworkCard> m_aCards = null; 
m_aCards = new ObservableCollection<NetworkCard>(oHelper.ListNetworkCards()); 
関連する問題