Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<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}/>"

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:
C#
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;
   }

The code behind for the StackPanel looks like this
C#
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));
            }
        }
    }
Posted

1 solution

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.
 
Share this answer
 
Comments
Chris Platsikoudis 31-Mar-15 4:25am    
Yeah, but include it how? Because I've tried instantiating an Errors object in the ConnectionViewModel class and then referencing its members, but it doesn't work (my XAML doesn't see it).
In the end probably I'll end up merging together the two classes (Errors and ConnectionViewModel), but isn't that kind of a "hack"? I mean what if I had 5 or 6 different classes that I wanted to reference, would I have to merge them all in a single class?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900