DataBinding C# ObservableCollection SerialPort to Combobox (SerialPort.getPortNames=>Method that return...











up vote
-1
down vote

favorite












I need to Bind a list of my Available ports in Computer to a Combobox in WPF. I am using ObservableCollection. I have done like






public class MainWindowVM
{
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }

public MainWindowVM()
{
ListOfAvaliablePorts = new ObservableCollection<String>();
ListOfAvaliablePorts.Add(SerialPort.GetPortNames());
}
}





But the output on the combobox Output is
String Array



My Xaml Code is






     <!--#region Combobox-->
<ComboBox
Name="portsCombobox" Width="80" Margin="50,0,0,0"
ItemsSource="{Binding ListOfAvaliablePorts}">
</ComboBox>
<!--#endregion-->





Please help me to solve the problem so that i can get the output like
COM1 COM2 COM6 COM7 COM5










share|improve this question






















  • then you need IEnumerable<string> not IEnumerable<string> obviously string is IEnumerable<string>... so why not public string ListOfAvaliablePorts => .... filling the dots is your homework edit: why it has +1?
    – Selvin
    Nov 8 at 3:20












  • .... public string ListOfAvaliablePorts => SerialPort.GetPortNames(); obviously would be enough
    – Selvin
    Nov 8 at 3:42















up vote
-1
down vote

favorite












I need to Bind a list of my Available ports in Computer to a Combobox in WPF. I am using ObservableCollection. I have done like






public class MainWindowVM
{
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }

public MainWindowVM()
{
ListOfAvaliablePorts = new ObservableCollection<String>();
ListOfAvaliablePorts.Add(SerialPort.GetPortNames());
}
}





But the output on the combobox Output is
String Array



My Xaml Code is






     <!--#region Combobox-->
<ComboBox
Name="portsCombobox" Width="80" Margin="50,0,0,0"
ItemsSource="{Binding ListOfAvaliablePorts}">
</ComboBox>
<!--#endregion-->





Please help me to solve the problem so that i can get the output like
COM1 COM2 COM6 COM7 COM5










share|improve this question






















  • then you need IEnumerable<string> not IEnumerable<string> obviously string is IEnumerable<string>... so why not public string ListOfAvaliablePorts => .... filling the dots is your homework edit: why it has +1?
    – Selvin
    Nov 8 at 3:20












  • .... public string ListOfAvaliablePorts => SerialPort.GetPortNames(); obviously would be enough
    – Selvin
    Nov 8 at 3:42













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I need to Bind a list of my Available ports in Computer to a Combobox in WPF. I am using ObservableCollection. I have done like






public class MainWindowVM
{
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }

public MainWindowVM()
{
ListOfAvaliablePorts = new ObservableCollection<String>();
ListOfAvaliablePorts.Add(SerialPort.GetPortNames());
}
}





But the output on the combobox Output is
String Array



My Xaml Code is






     <!--#region Combobox-->
<ComboBox
Name="portsCombobox" Width="80" Margin="50,0,0,0"
ItemsSource="{Binding ListOfAvaliablePorts}">
</ComboBox>
<!--#endregion-->





Please help me to solve the problem so that i can get the output like
COM1 COM2 COM6 COM7 COM5










share|improve this question













I need to Bind a list of my Available ports in Computer to a Combobox in WPF. I am using ObservableCollection. I have done like






public class MainWindowVM
{
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }

public MainWindowVM()
{
ListOfAvaliablePorts = new ObservableCollection<String>();
ListOfAvaliablePorts.Add(SerialPort.GetPortNames());
}
}





But the output on the combobox Output is
String Array



My Xaml Code is






     <!--#region Combobox-->
<ComboBox
Name="portsCombobox" Width="80" Margin="50,0,0,0"
ItemsSource="{Binding ListOfAvaliablePorts}">
</ComboBox>
<!--#endregion-->





Please help me to solve the problem so that i can get the output like
COM1 COM2 COM6 COM7 COM5






public class MainWindowVM
{
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }

public MainWindowVM()
{
ListOfAvaliablePorts = new ObservableCollection<String>();
ListOfAvaliablePorts.Add(SerialPort.GetPortNames());
}
}





public class MainWindowVM
{
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }

public MainWindowVM()
{
ListOfAvaliablePorts = new ObservableCollection<String>();
ListOfAvaliablePorts.Add(SerialPort.GetPortNames());
}
}





     <!--#region Combobox-->
<ComboBox
Name="portsCombobox" Width="80" Margin="50,0,0,0"
ItemsSource="{Binding ListOfAvaliablePorts}">
</ComboBox>
<!--#endregion-->





     <!--#region Combobox-->
<ComboBox
Name="portsCombobox" Width="80" Margin="50,0,0,0"
ItemsSource="{Binding ListOfAvaliablePorts}">
</ComboBox>
<!--#endregion-->






c# wpf combobox binding serial-port






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 3:01









mechbaral

224




224












  • then you need IEnumerable<string> not IEnumerable<string> obviously string is IEnumerable<string>... so why not public string ListOfAvaliablePorts => .... filling the dots is your homework edit: why it has +1?
    – Selvin
    Nov 8 at 3:20












  • .... public string ListOfAvaliablePorts => SerialPort.GetPortNames(); obviously would be enough
    – Selvin
    Nov 8 at 3:42


















  • then you need IEnumerable<string> not IEnumerable<string> obviously string is IEnumerable<string>... so why not public string ListOfAvaliablePorts => .... filling the dots is your homework edit: why it has +1?
    – Selvin
    Nov 8 at 3:20












  • .... public string ListOfAvaliablePorts => SerialPort.GetPortNames(); obviously would be enough
    – Selvin
    Nov 8 at 3:42
















then you need IEnumerable<string> not IEnumerable<string> obviously string is IEnumerable<string>... so why not public string ListOfAvaliablePorts => .... filling the dots is your homework edit: why it has +1?
– Selvin
Nov 8 at 3:20






then you need IEnumerable<string> not IEnumerable<string> obviously string is IEnumerable<string>... so why not public string ListOfAvaliablePorts => .... filling the dots is your homework edit: why it has +1?
– Selvin
Nov 8 at 3:20














.... public string ListOfAvaliablePorts => SerialPort.GetPortNames(); obviously would be enough
– Selvin
Nov 8 at 3:42




.... public string ListOfAvaliablePorts => SerialPort.GetPortNames(); obviously would be enough
– Selvin
Nov 8 at 3:42












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










initalize the ObservableCollection of string and give the list of ports.



public ObservableCollection<String> ListOfAvaliablePorts { get; set; }


ListOfAvaliablePorts = new ObservableCollection<String>(SerialPort.GetPortNames());





share|improve this answer























  • using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
    – Selvin
    Nov 8 at 3:41












  • Corrected the ToList<String> to toList().
    – Satish Pai
    Nov 8 at 3:56










  • there is no need to call ToList() at all
    – Selvin
    Nov 8 at 3:57










  • Yes no need to call ToList(), it shows the list of Comport without using ToList()
    – mechbaral
    Nov 8 at 4:10




















up vote
0
down vote













If you want to bind a string array, then it just ObservableCollection . And you also need to implement INotifyPropertyChanged to raise the property change.



You can take a look as my solution as bellow:



public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private ObservableCollection<string> _ListOfAvaliablePorts;

public ObservableCollection<string> ListOfAvaliablePorts
{
get
{
return _ListOfAvaliablePorts;
}
set
{
if (value != _ListOfAvaliablePorts)
{
_ListOfAvaliablePorts = value;
OnPropertyChanged(nameof(ListOfAvaliablePorts));
}
}
}

public MainWindowVM()
{
var comPorts = SerialPort.GetPortNames();
_ListOfAvaliablePorts = new ObservableCollection<string>(comPorts);
}
}





share|improve this answer





















  • Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
    – mechbaral
    Nov 8 at 4:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200941%2fdatabinding-c-sharp-observablecollection-serialport-to-combobox-serialport-getp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










initalize the ObservableCollection of string and give the list of ports.



public ObservableCollection<String> ListOfAvaliablePorts { get; set; }


ListOfAvaliablePorts = new ObservableCollection<String>(SerialPort.GetPortNames());





share|improve this answer























  • using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
    – Selvin
    Nov 8 at 3:41












  • Corrected the ToList<String> to toList().
    – Satish Pai
    Nov 8 at 3:56










  • there is no need to call ToList() at all
    – Selvin
    Nov 8 at 3:57










  • Yes no need to call ToList(), it shows the list of Comport without using ToList()
    – mechbaral
    Nov 8 at 4:10

















up vote
0
down vote



accepted










initalize the ObservableCollection of string and give the list of ports.



public ObservableCollection<String> ListOfAvaliablePorts { get; set; }


ListOfAvaliablePorts = new ObservableCollection<String>(SerialPort.GetPortNames());





share|improve this answer























  • using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
    – Selvin
    Nov 8 at 3:41












  • Corrected the ToList<String> to toList().
    – Satish Pai
    Nov 8 at 3:56










  • there is no need to call ToList() at all
    – Selvin
    Nov 8 at 3:57










  • Yes no need to call ToList(), it shows the list of Comport without using ToList()
    – mechbaral
    Nov 8 at 4:10















up vote
0
down vote



accepted







up vote
0
down vote



accepted






initalize the ObservableCollection of string and give the list of ports.



public ObservableCollection<String> ListOfAvaliablePorts { get; set; }


ListOfAvaliablePorts = new ObservableCollection<String>(SerialPort.GetPortNames());





share|improve this answer














initalize the ObservableCollection of string and give the list of ports.



public ObservableCollection<String> ListOfAvaliablePorts { get; set; }


ListOfAvaliablePorts = new ObservableCollection<String>(SerialPort.GetPortNames());






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 8 at 3:58

























answered Nov 8 at 3:39









Satish Pai

39619




39619












  • using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
    – Selvin
    Nov 8 at 3:41












  • Corrected the ToList<String> to toList().
    – Satish Pai
    Nov 8 at 3:56










  • there is no need to call ToList() at all
    – Selvin
    Nov 8 at 3:57










  • Yes no need to call ToList(), it shows the list of Comport without using ToList()
    – mechbaral
    Nov 8 at 4:10




















  • using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
    – Selvin
    Nov 8 at 3:41












  • Corrected the ToList<String> to toList().
    – Satish Pai
    Nov 8 at 3:56










  • there is no need to call ToList() at all
    – Selvin
    Nov 8 at 3:57










  • Yes no need to call ToList(), it shows the list of Comport without using ToList()
    – mechbaral
    Nov 8 at 4:10


















using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
– Selvin
Nov 8 at 3:41






using string.ToList<String> is redundant ... using string.ToList() when IEnumerable<string> is expected is also redundant
– Selvin
Nov 8 at 3:41














Corrected the ToList<String> to toList().
– Satish Pai
Nov 8 at 3:56




Corrected the ToList<String> to toList().
– Satish Pai
Nov 8 at 3:56












there is no need to call ToList() at all
– Selvin
Nov 8 at 3:57




there is no need to call ToList() at all
– Selvin
Nov 8 at 3:57












Yes no need to call ToList(), it shows the list of Comport without using ToList()
– mechbaral
Nov 8 at 4:10






Yes no need to call ToList(), it shows the list of Comport without using ToList()
– mechbaral
Nov 8 at 4:10














up vote
0
down vote













If you want to bind a string array, then it just ObservableCollection . And you also need to implement INotifyPropertyChanged to raise the property change.



You can take a look as my solution as bellow:



public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private ObservableCollection<string> _ListOfAvaliablePorts;

public ObservableCollection<string> ListOfAvaliablePorts
{
get
{
return _ListOfAvaliablePorts;
}
set
{
if (value != _ListOfAvaliablePorts)
{
_ListOfAvaliablePorts = value;
OnPropertyChanged(nameof(ListOfAvaliablePorts));
}
}
}

public MainWindowVM()
{
var comPorts = SerialPort.GetPortNames();
_ListOfAvaliablePorts = new ObservableCollection<string>(comPorts);
}
}





share|improve this answer





















  • Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
    – mechbaral
    Nov 8 at 4:05















up vote
0
down vote













If you want to bind a string array, then it just ObservableCollection . And you also need to implement INotifyPropertyChanged to raise the property change.



You can take a look as my solution as bellow:



public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private ObservableCollection<string> _ListOfAvaliablePorts;

public ObservableCollection<string> ListOfAvaliablePorts
{
get
{
return _ListOfAvaliablePorts;
}
set
{
if (value != _ListOfAvaliablePorts)
{
_ListOfAvaliablePorts = value;
OnPropertyChanged(nameof(ListOfAvaliablePorts));
}
}
}

public MainWindowVM()
{
var comPorts = SerialPort.GetPortNames();
_ListOfAvaliablePorts = new ObservableCollection<string>(comPorts);
}
}





share|improve this answer





















  • Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
    – mechbaral
    Nov 8 at 4:05













up vote
0
down vote










up vote
0
down vote









If you want to bind a string array, then it just ObservableCollection . And you also need to implement INotifyPropertyChanged to raise the property change.



You can take a look as my solution as bellow:



public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private ObservableCollection<string> _ListOfAvaliablePorts;

public ObservableCollection<string> ListOfAvaliablePorts
{
get
{
return _ListOfAvaliablePorts;
}
set
{
if (value != _ListOfAvaliablePorts)
{
_ListOfAvaliablePorts = value;
OnPropertyChanged(nameof(ListOfAvaliablePorts));
}
}
}

public MainWindowVM()
{
var comPorts = SerialPort.GetPortNames();
_ListOfAvaliablePorts = new ObservableCollection<string>(comPorts);
}
}





share|improve this answer












If you want to bind a string array, then it just ObservableCollection . And you also need to implement INotifyPropertyChanged to raise the property change.



You can take a look as my solution as bellow:



public class MainWindowVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private ObservableCollection<string> _ListOfAvaliablePorts;

public ObservableCollection<string> ListOfAvaliablePorts
{
get
{
return _ListOfAvaliablePorts;
}
set
{
if (value != _ListOfAvaliablePorts)
{
_ListOfAvaliablePorts = value;
OnPropertyChanged(nameof(ListOfAvaliablePorts));
}
}
}

public MainWindowVM()
{
var comPorts = SerialPort.GetPortNames();
_ListOfAvaliablePorts = new ObservableCollection<string>(comPorts);
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 3:35









Nhan Phan

7501522




7501522












  • Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
    – mechbaral
    Nov 8 at 4:05


















  • Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
    – mechbaral
    Nov 8 at 4:05
















Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
– mechbaral
Nov 8 at 4:05




Thank you it shows the data as required. But When i removed the Com port it didn't update the list though. Anyway restarting program will do a work for me. Thank you for your help. Have a time
– mechbaral
Nov 8 at 4:05


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53200941%2fdatabinding-c-sharp-observablecollection-serialport-to-combobox-serialport-getp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini