如何将可用的SerialPort名称添加到RibbonComboBox或RibbonGallery [英] How to add available SerialPort names to RibbonComboBox or RibbonGallery
本文介绍了如何将可用的SerialPort名称添加到RibbonComboBox或RibbonGallery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用以下代码,我将可用的串行端口名称添加到RibbonGallery对象:
xaml:
With following code I add available serial port names to the RibbonGallery object:
xaml:
<ribbon:RibbonComboBox Name="cmbBoxComPortName" SelectionBoxWidth="40" Label="Port name:">
<ribbon:RibbonGallery Name="galleryComPortName" SelectionChanged="galleryComPortName_SelectionChanged" />
</ribbon:RibbonComboBox>
C#背后的代码:
code-behind c#:
galleryComPortName.ItemsSource = SerialPortAdapter.GetAvailablePortList();
SerialPortAdapter代码:
SerialPortAdapter code:
class SerialPortAdapter
{
public static List<string> GetAvailablePortList()
{
var portList = SerialPort.GetPortNames().ToList();
portList.Sort();
return portList;
}
}
现在的问题是,端口名称出现在组合框中,但无法选择.
The problem is now, that the port names are present in the combobox but they are not selectable.
How can I select them and treat the event to set my port name?
推荐答案
我对Ribbon:RibbonComboBox控制器有相同的问题.您的专案是.Net 3.5吗?那可能是造成我的问题.
我的解决方法是使用原始的ComboBox控制器而不是Ribbon:RibbonComboBox.
I had the same problem with Ribbon:RibbonComboBox controller. Is your project .Net 3.5? That might be causing my problems.
My workaround was to use the original ComboBox controller instead of Ribbon:RibbonComboBox.
抱歉,这不是解决方案.在前面的答案之后,不知道添加更多代码的方法.
现在,我尝试执行以下操作,但不幸的是得到了相同的结果.
SerialPortAdapter中的代码:
Sorry this, isn''t a solution. Don''t know a way to add more code after the previous answers.
Now I tried following, but unfortunatelly with the the same result.
Code in SerialPortAdapter:
public static ObservableCollection<RibbonGalleryItem> GetAvailablePortList()
{
var portList = SerialPort.GetPortNames().ToList();
portList.Sort();
var portObsCol = new ObservableCollection<RibbonGalleryItem>();
foreach (var rgi in portList.Select(port => new RibbonGalleryItem() { Content = port, Name = port }))
{
portObsCol.Add(rgi);
}
return portObsCol;
}
以这种方式发现了,这是可行的.
xaml:
Found out this way, which is working.
xaml:
<ribbon:RibbonComboBox Name="cmbBoxComPortNameRibbon" SelectionBoxWidth="40" Label="Port name:">
<ribbon:RibbonGallery Name="galleryComPortName" SelectionChanged="GalleryComPortNameSelectionChanged" SelectedValuePath="Content">
<ribbon:RibbonGalleryCategory Name="galleryCategoryComPortName"/>
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
C#背后的代码:
code-behind C#:
galleryCategoryComPortName.ItemsSource = SerialPortAdapter.GetAvailablePortList();
SerialPortAdapter代码:
SerialPortAdapter code:
public static ObservableCollection<RibbonGalleryItem> GetAvailablePortList()
{
var portList = SerialPort.GetPortNames().ToList();
portList.Sort();
var portObsCol = new ObservableCollection<RibbonGalleryItem>();
foreach (var rgi in portList.Select(port => new RibbonGalleryItem() { Content = port, Name = port }))
{
portObsCol.Add(rgi);
}
return portObsCol;
}
这篇关于如何将可用的SerialPort名称添加到RibbonComboBox或RibbonGallery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文