C# 组合框绑定源
Im有以下wpf窗口:C# 组合框绑定源,c#,wpf,serial-port,C#,Wpf,Serial Port,Im有以下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.Por
<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>
如果您想在XAML中填充其他
组合框
,那么对于Enum
,您还可以创建ObjectDataProvider
,就像您对COM端口所做的那样:
<ObjectDataProvider x:Key="HandshakeValues" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ports:Handshake" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ComboBox ItemsSource="{Binding Source={StaticResource HandshakeValues}}"/>
其中
sys
是xmlns:sys=“clr namespace:System;assembly=mscorlib”
还有什么组合框?@DaniDărăban,你是说在XAML中吗?我有4个组合框,我设法填充Com端口,但其他3个我不知道如何填充,是的im xml代码这是我的费率组合框:如果粘贴item.source内容,我会遇到一些错误
<ObjectDataProvider x:Key="HandshakeValues" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="ports:Handshake" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ComboBox ItemsSource="{Binding Source={StaticResource HandshakeValues}}"/>
<ComboBox>
<ComboBox.ItemsSource>
<x:Array Type="{x:Type sys:Int32}">
<sys:Int32>9600</sys:Int32>
<sys:Int32>14400</sys:Int32>
<sys:Int32>19200</sys:Int32>
<sys:Int32>38400</sys:Int32>
<sys:Int32>57600</sys:Int32>
<sys:Int32>115200</sys:Int32>
<sys:Int32>128000</sys:Int32>
</x:Array>
</ComboBox.ItemsSource>
</ComboBox>