2013-05-02 48 views
2

我想綁定一個字典到組合框,但在UI上沒有顯示的項目只有空白項是綁定。請找到下面的代碼,讓我知道我做錯了什麼?綁定字典到組合框

代碼:

private Dictionary<string, string> _timeToExpirationValues = 
new Dictionary<string, string>  { "< 15 Days", "15" }, 
            { "< 30 Days", "30" }, 
            { "< 60 Days", "60" }, 
            { "< 90 Days", "90" }, 
            { "< 1 year", "365" }}; 


     /// <summary> 
     /// Gets or sets the filter settings data. 
     /// </summary> 
     /// <value> 
     /// The filter settings data. 
     /// </value> 
     public Dictionary<string, string> TimeToExpirationValues 
     { 
      get { return _timeToExpirationValues; } 
      set { SetProperty(ref _timeToExpirationValues, value); } 
     } 

XAML代碼:

<ComboBox Grid.Row="1" Grid.Column="1" 
            ItemsSource="{Binding TimeToExpirationValues}" 
            DisplayMemberPath="Key" 
            SelectedValuePath="Value" /> 

是被顯示的內容是如下:

enter image description here

回答

0

試試這個(請注意多字Path):

<ComboBox Grid.Row="1" Grid.Column="1" 
           ItemsSource="{Binding Path=TimeToExpirationValues}" 
           DisplayMemberPath="Key" 
           SelectedValuePath="Value" /> 
+0

我想這也而非工作 – 2013-05-02 08:51:28

+0

其實我可以看到,那些許多項目都被綁定,但在用戶界面上不顯示? – 2013-05-02 08:52:54

1

您是否將DataContext設置爲窗口或組合框?如果不是,你應該添加這些行到你的窗口

<Window.DataContext> 
     <local:YourClassName/> 
    </Window.DataContext> 
0

我試過你的例子,它在這裏工作。

所以我認爲,你對combox中的標籤只有一個白色的前景。 (也許在app.xaml中聲明爲樣式?)。