2009-10-22 10 views
40

voglio riempire una casella combinata con i dati chiave/valore nel codice dietro, ho questo:Come associare un ComboBox Dizionario generico tramite ObjectDataProvider

XAML:

<Window x:Class="TestCombo234.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TestCombo234" 
    Title="Window1" Height="300" Width="300"> 
    <Window.Resources> 
     <ObjectDataProvider x:Key="Choices" ObjectType="{x:Type local:CollectionData}" MethodName="GetChoices"/> 
    </Window.Resources> 
    <StackPanel HorizontalAlignment="Left"> 
     <ComboBox ItemsSource="{Binding Source={StaticResource Choices}}"/> 
    </StackPanel> 
</Window> 

codice sottostante :

using System.Windows; 
using System.Collections.Generic; 

namespace TestCombo234 
{ 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 
    } 

    public static class CollectionData 
    { 
     public static Dictionary<int, string> GetChoices() 
     { 
      Dictionary<int, string> choices = new Dictionary<int, string>(); 
      choices.Add(1, "monthly"); 
      choices.Add(2, "quarterly"); 
      choices.Add(3, "biannually"); 
      choices.Add(4, "yearly"); 
      return choices; 
     } 
    } 
} 

Ma questo mi dà questo:

alt text http://img193.imageshack.us/img193/9218/choices.png

Cosa devo cambiare in modo che la chiave è l'int e il valore è la stringa?

+0

Google rivela questa: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/6f9ff9a9-9133-40cc-bfdb-a47d340658bf – Heinzi

+1

si presenta come il l'immagine nella tua domanda sopra è rotta (e ora [mostrando invece un annuncio] (http://meta.stackexchange.com/questions/263771/ban-imageshack-images-because-the-re-reusing-old-urls-for -pubblicità)). Potresti caricare nuovamente l'immagine su stack.imgur o modificare la tua domanda per rimuoverla? –

risposta

98

Per la tua casella combinata aggiungere

SelectedValuePath="Key" DisplayMemberPath="Value" 
+2

Penso che tu intenda SelectedValuePath e DisplayMemberPath, quelli almeno hanno funzionato per me, grazie. –

+0

Oops, sì, l'ho fatto. Risolverò la risposta. –

+3

Sei il mio eroe! –

4

C'è un modo più semplice.

Convertire l'enumerazione in un oggetto Generic.Dictionary. Per esempio diciamo che si voleva una casella combinata con il giorno della settimana (solo convertire VB a C#)

Dim colWeekdays As New Generic.Dictionary(Of FirstDayOfWeek, String) 
    For intWeekday As FirstDayOfWeek = vbSunday To vbSaturday 
     colWeekdays.Add(intWeekday, WeekdayName(intWeekday)) 
    Next 

RadComboBox_Weekdays.ItemsSource = colWeekdays 

Nel vostro XAML è sufficiente impostare la seguente per associare a un oggetto:

SelectedValue="{Binding Path= StartDayNumberOfWeeek}" SelectedValuePath="Key" 
DisplayMemberPath="Value" /> 

Il codice sopra può essere facilmente generalizzato usando il reflection per gestire qualsiasi enumerazione.

speranza che questo aiuta

Problemi correlati