如何在WPF中从组合框中选择com端口? [英] How do I select the com port from a combobox in WPF?

查看:183
本文介绍了如何在WPF中从组合框中选择com端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说我有一个WPF应用程序通过串口与USB继电器通信,并且(因为COM端口根据你连接它的USB端口而改变)我希望有一个组合框来选择合适的一。我有点找到了一种方法,可以按照上的说明将组合框绑定到列表中此链接 [ ^ 。然而,我的问题有两个:a)我真的不明白用户选择如何反映到代码隐藏和b)这迫使我改变已经设置为另一个源(对象)的XAML的数据上下文使用一些简单的计数器)从而打破旧数据绑定。



我的代码太大而无法粘贴,但如果需要我可以粘贴它。然而,XAML中的相关部分如下:

As the title says I have a WPF application which communicates to a USB relay via a serial port, and (because the COM port changes depending on which USB port you connect it to) I want to have a combobox to select the appropriate one. I have sort of found a way to bind the combobox to a list following the instructions on this link[^]. My problem however is twofold: a) I don't really understand how the user choice is reflected back to the code-behind and b) this forces me to change the data context of my XAML which is already set to another source (an object with some simple counters) and thus breaks the old data bindings.

My code would be too big to paste, but if needed I can paste it. The relevant parts however in the XAML are the following:

<StackPanel DataContext="StaticResource Errors">
    <Label x:Name="errorDisplayTitle" Content="Total Errors"/>
<StackPanel/>
<ComboBox ItemsSource="{Binding Path=SerialPortParamsEntries}"
          DisplayMemberPath="Name"
          SelectedValuePath="Name"
          SelectedValue="{Binding Path=SerialPortParamsEntry}/>"



所以我最大的问题是ComboxBox和StackPanel需要完全不同的DataContext。

对于ComboBox,背后的代码看起来像这样:


So my biggest problem is that the ComboxBox and the StackPanel need to have entirely different DataContext.
For the ComboBox the code behind looks sth like this:

public class ConnectionViewModel : INotifyPropertyChanged
   {


   public class SerialPortParams
   {
       public string Name { get; set; }


       public SerialPortParams(string name)
       {
          Name = name;
       }
   }
    public ConnectionViewModel()
    {
           IList<SerialPortParams> list = new List<SerialPortParams>();
           string[] ports = SerialPort.GetPortNames();
           foreach (string port in ports)
           {
               list.Add(new SerialPortParams(port));
           }
           _SerialPortParamsEntries = new CollectionView(list);
       }
       private readonly CollectionView _SerialPortParamsEntries;
       private string _SerialPortParamsEntry;
       public CollectionView SerialPortParamsEntries
       {
           get { return _SerialPortParamsEntries; }
       }
       public string SerialPortParamsEntry
       {
           get { return _SerialPortParamsEntry; }
           set
           {
               if (_SerialPortParamsEntry == value) return;
               _SerialPortParamsEntry = value;
               OnPropertyChanged("SerialPortParamsEntry");
           }
       }

       private void OnPropertyChanged(string propertyName)
       {
           if (PropertyChanged != null)
               PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
       }
       public event PropertyChangedEventHandler PropertyChanged;
   }



StackPanel背后的代码如下所示


The code behind for the StackPanel looks like this

public class Errors : INotifyPropertyChanged
    {  private int _totalTouches;
        // Declare the PropertyChanged event.
        public event PropertyChangedEventHandler PropertyChanged;

        public int totalErrors
        {
            get { return _totalErrors; }
            set
            {
                _totalErrors = value;
                // Call NotifyPropertyChanged when the source property 
                // is updated.
                NotifyPropertyChanged("totalErrors");
            }

        } 
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }

推荐答案

没有WPF专家,但不是ViewModel一个专门用于将视图的所有位组合在一起的类,即您只需要在ConnectionViewModel中包含totalErrors。
No expert on WPF, but isn't the ViewModel a class specifically used to bring together all the bits for a view, i.e. you just need to include the totalErrors in your ConnectionViewModel.


这篇关于如何在WPF中从组合框中选择com端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆