我想将可用串行端口列表绑定(bind)到组合框。目前,我手动添加可用的串行端口。就像这样,
foreach (string s in SerialPort.GetPortNames())
{
ComboBoxItem cbi = new ComboBoxItem();
cbi.Content = s;
myComboBox.Items.Add(cbi);
}
myComboBox 是我的组合框名称。我该如何进行绑定(bind)?谢谢。
最佳答案
您可以使用 ObjectDataProvider 绑定(bind)到方法。
<Window x:Class="SerialPortBinding.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 ObjectType="{x:Type ports:SerialPort}" MethodName="GetPortNames" x:Key="portNames"/>
</Window.Resources>
<ComboBox ItemsSource="{Binding Source={StaticResource portNames}}"/>
</Window>
关于wpf - 串行端口 WPF ComboBox 数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15495543/