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
c# wpf combobox binding serial-port
add a comment |
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
c# wpf combobox binding serial-port
then you needIEnumerable<string>
notIEnumerable<string>
obviouslystring
isIEnumerable<string>
... so why notpublic 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
add a comment |
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
c# wpf combobox binding serial-port
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
c# wpf combobox binding serial-port
asked Nov 8 at 3:01
mechbaral
224
224
then you needIEnumerable<string>
notIEnumerable<string>
obviouslystring
isIEnumerable<string>
... so why notpublic 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
add a comment |
then you needIEnumerable<string>
notIEnumerable<string>
obviouslystring
isIEnumerable<string>
... so why notpublic 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
add a comment |
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());
usingstring.ToList<String>
is redundant ... usingstring.ToList()
whenIEnumerable<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
add a comment |
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);
}
}
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
add a comment |
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());
usingstring.ToList<String>
is redundant ... usingstring.ToList()
whenIEnumerable<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
add a comment |
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());
usingstring.ToList<String>
is redundant ... usingstring.ToList()
whenIEnumerable<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
add a comment |
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());
initalize the ObservableCollection of string and give the list of ports.
public ObservableCollection<String> ListOfAvaliablePorts { get; set; }
ListOfAvaliablePorts = new ObservableCollection<String>(SerialPort.GetPortNames());
edited Nov 8 at 3:58
answered Nov 8 at 3:39
Satish Pai
39619
39619
usingstring.ToList<String>
is redundant ... usingstring.ToList()
whenIEnumerable<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
add a comment |
usingstring.ToList<String>
is redundant ... usingstring.ToList()
whenIEnumerable<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
add a comment |
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);
}
}
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
add a comment |
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);
}
}
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
add a comment |
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);
}
}
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);
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
then you need
IEnumerable<string>
notIEnumerable<string>
obviouslystring
isIEnumerable<string>
... so why notpublic 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