我有以下 wpf 窗口:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ports="clr-namespace:System.IO.Ports;assembly=System"
Title="MainWindow"
SizeToContent="WidthAndHeight">
<Window.Resources>
<ObjectDataProvider x:Key="portNames"
MethodName="GetPortNames"
ObjectType="{x:Type ports:SerialPort}" />
</Window.Resources>
<ComboBox Name="cbox" ItemsSource="{Binding Source={StaticResource portNames}}" SelectionChanged="ComboBox_SelectionChanged" />
</Window>
我正在使用 ObjectDataProvider 来填充 Com Port 组合框。但我不知道如何填充其他组合框。使用的 C# 函数:
private void LoadBaudRates()
{
cboxBaudRate.DataContext = new int[] { 9600, 14400, 19200, 38400, 57600, 115200, 128000 };
cboxBaudRate.SelectedIndex = 0;
}
private void LoadParity()
{
cboxParity.DataContext = Enum.GetValues(typeof(Parity));
}
private void LoadFlowControl()
{
cboxFlowControl.DataContext = Enum.GetValues(typeof(Handshake));
}
private void LoadCOMPorts()
{
var comPorts = SerialPort.GetPortNames();
//cboxCOMPorts.DataContext = comPorts;
this.cboxCOMPorts.DataContext = comPorts;
}