Pergunta

i've used a method,

public static string[] getMyStrings()

and passed it to a combobox like:

cmbMyBox.itemsSource  = getMyStrings(). //(more detail below)

the debugger shows the strings have been added to the combobox, but when i look in the combobox the number of entries are there, but blank. anyone know what i'm doing wrong?

my list of devices:

public static string[] GetMIDIInDevices()
    {
        //get list of devices
        string[] returnDevices = new string[MidiIn.NumberOfDevices];

        // Get the product name for each device found
        for (int device = 0; device < MidiIn.NumberOfDevices; device++)
        {
            returnDevices[device] = MidiIn.DeviceInfo(device).ProductName;
        }
        return returnDevices;
    }   

The simple code to display it on my main window in WPF:

public MainWindow()
    {
        InitializeComponent();
        cmbMidiDropdown.ItemsSource = NAudioMIDI.GetMIDIInDevices();
        //LoadMidiInDevicesIntoComboBox();
    }

here's the XAML:

<TabItem Header="MIDI Settings" Name="tabMidiSettings" Background="DarkGoldenrod">
            <Grid  Background="Honeydew">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="758" />
                </Grid.ColumnDefinitions>
                <ComboBox Grid.Column="1" Height="23" HorizontalAlignment="Right" Margin="0,141,146,0" Name="cmbMidiDropdown" VerticalAlignment="Top" Width="312"  ItemsSource="{Binding}" DisplayMemberPath="Name" />
                <Label Content="Select Midi Input device" Grid.Column="1" Height="28" HorizontalAlignment="Left" Margin="143,141,0,0" Name="label1" VerticalAlignment="Top" />
            </Grid>
        </TabItem>

DEBUGGER OUTPUT:

GetMIDIInDevices()

-       returnDevices   {string[3]} string[]
    [0] "MIDISPORT 2x2 In A"    string
    [1] "MIDISPORT 2x2 In B"    string
    [2] "Turtle Beach USB MIDI 1x1" string
    returnDevices[device]   "Turtle Beach USB MIDI 1x1" string

cmbMIDIDropdown

+       cmbMidiDropdown {System.Windows.Controls.ComboBox Items.Count:3}        
-       cmbMidiDropdown.ItemsSource {string[3]}      
-       [string[]]  {string[3]} string[]
    [0] "MIDISPORT 2x2 In A"    string
    [1] "MIDISPORT 2x2 In B"    string
    [2] "Turtle Beach USB MIDI 1x1" string
Foi útil?

Solução

You are setting the ItemsSource in XAML, and the DisplayMemberPath as well. When you set the ItemsSource in code, you are not changing the DisplayMemberPath, so the combobox is trying to call each object's .Name property. String does not have a .Name, so you are getting blanks. Remove the ItemsSource and DisplayMemberPath from your xaml, and you should see the values you expect.

Outras dicas

Try this...

cmbMidiDropdown.DataContext = NAudioMIDI.GetMIDIInDevices();

You're setting your ItemsSource to inherit the DataContext by setting the {Binding} on the ItemsSource property. In doing this simply setting the DataContext of your ComboBox to the listing of strings will allow the ComboxBox to populate accordingly.

Also, setting your DisplayMemberPath is not needed since you are setting the ItemsSource as a listing of strings versus complex objects and a Name property does not exist on the string class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top