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
You are setting the ItemsSource in XAML, and the
DisplayMemberPathas well. When you set the ItemsSource in code, you are not changing the DisplayMemberPath, so the combobox is trying to call each object’s.Nameproperty.Stringdoes not have a.Name, so you are getting blanks. Remove theItemsSourceandDisplayMemberPathfrom your xaml, and you should see the values you expect.