我使用了一种方法,
public static string[] getMyStrings()
并将其传递给类似的组合框:
cmbMyBox.itemsSource = getMyStrings(). //(more detail below)
调试器显示字符串已添加到组合框中,但是当我查看组合框时,条目数是多少,但是空白。谁知道我做错了什么?
我的设备列表:
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;
}
在WPF的主窗口中显示它的简单代码:
public MainWindow()
{
InitializeComponent();
cmbMidiDropdown.ItemsSource = NAudioMIDI.GetMIDIInDevices();
//LoadMidiInDevicesIntoComboBox();
}
这是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
答案 0 :(得分:1)
您正在设置XAML中的ItemsSource,以及DisplayMemberPath
。在代码中设置ItemsSource时,您不会更改DisplayMemberPath,因此组合框会尝试调用每个对象的.Name
属性。 String
没有.Name
,因此您将获得空白。从您的xaml中删除ItemsSource
和DisplayMemberPath
,您应该会看到您期望的值。
答案 1 :(得分:0)
试试这个......
cmbMidiDropdown.DataContext = NAudioMIDI.GetMIDIInDevices();
您通过在ItemsSource
媒体资源上设置DataContext
,将{Binding}
设为继承ItemsSource
。在执行此操作时,只需将DataContext
的{{1}}设置为字符串列表,即可ComboBox
进行相应填充。
此外,由于您将ComboxBox
设置为字符串与复杂对象的列表,并且字符串类上不存在DisplayMemberPath
属性,因此不需要设置ItemsSource
。< / p>