0
votes

I have a combobox that holds all the serial port names found on my system. The bindings to load and select the item seem to work fine. The issue is when I perform an action that changes the list (through a Refresh button), the combobox reflects the updated list but the selected Text is no longer what it was before the refresh (the selected value is not the one that was changed). I get an empty space. [See attached picture comparison] I tried adding another binding for the Text exclusively, but same result. Any ideas what might be wrong? XAML code

<ComboBox x:Name="cbxPorts" ItemsSource="{Binding PortNames}" 
            Text="{Binding cbxPortTextContent, Mode=TwoWay}" 
            SelectedItem="{Binding SelectedComPort, Mode=TwoWay}" 
            HorizontalAlignment="Left" Margin="10,23,0,0" VerticalAlignment="Top" 
            Width="119"/>

Model code

   // Property for Com Port Selection
    public String SelectedComPort
    {
        get { return selectedComPort; }
        set
        {
            if (value != null)
            {
                selectedComPort = value;
                Properties.Settings.Default.ComPort = selectedComPort;
                Properties.Settings.Default.Save();
            }
            else
            {

            }
        }
    }
    public String CbxPortTextContent
    {
        get { return cbxPortTextContent; }
        set
        {
            cbxPortTextContent = value;
            OnPropertyChanged("CbxPortTextContent");
        }
    }

This is first usable WPF MVVM project. All help/suggestions will be appreciated

1

1 Answers

0
votes

On my opinion you can try to use another properties of comboBox: DisplayMemberPath="value" SelectedValuePath="Key".

For exemple:

public class PortNames
{
  public string value{get;set;}
  public int key {get;set;}
}

ctor()
{
  cbxPorts.ItemsSource = new List<PortNames>();
}

In your XAML:

<ComboBox x:Name="cbxPorts" ItemsSource="{Binding PortNames}"             
            SelectedItem="{Binding SelectedComPort, Mode=TwoWay}" 
 HorizontalAlignment="Left" Margin="10,23,0,0"  SelectedValuePath="Key" DisplayMemberPath="value" VerticalAlignment="Top"  Width="119"/>