ComboBox ItemSourceProperty losts binding when item selected
up vote
0
down vote
favorite
I spend few hours on this working on bigger project, so I have made simple example. Problem is when you press "Add" button, it adds numbers to ComboBox item source property...Great, but when you open or select any item from comboBox, binding stops working.
I must be missing something.
XAML:
....
<Grid>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0"
VerticalAlignment="Top" Width="120"/>
<Button x:Name="AddButton" Content="Add" HorizontalAlignment="Left"
Margin="82,143,0,0" VerticalAlignment="Top" Width="75"
Click="NewNumberClick"/>
</Grid>
...
C# code:
namespace ComboBoxBinding
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<double> _numbers;
Binding comboBoxBinding;
public List<double> Numbers
{
get
{
return _numbers;
}
set
{
_numbers = value;
OnPropertyChanged("Numbers");
}
}
public MainWindow()
{
InitializeComponent();
Numbers = new List<double>(){ 1.0, 2.0, 3.0};
comboBoxBinding = new Binding();
comboBoxBinding.Path = new PropertyPath("Numbers");
comboBoxBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, comboBoxBinding);
DataContext = this;
}
private void NewNumberClick(object sender, RoutedEventArgs e)
{
Random rand = new Random();
double newNumber = 2.0 - rand.NextDouble();
Numbers.Add(newNumber);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
c# wpf combobox binding
New contributor
add a comment |
up vote
0
down vote
favorite
I spend few hours on this working on bigger project, so I have made simple example. Problem is when you press "Add" button, it adds numbers to ComboBox item source property...Great, but when you open or select any item from comboBox, binding stops working.
I must be missing something.
XAML:
....
<Grid>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0"
VerticalAlignment="Top" Width="120"/>
<Button x:Name="AddButton" Content="Add" HorizontalAlignment="Left"
Margin="82,143,0,0" VerticalAlignment="Top" Width="75"
Click="NewNumberClick"/>
</Grid>
...
C# code:
namespace ComboBoxBinding
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<double> _numbers;
Binding comboBoxBinding;
public List<double> Numbers
{
get
{
return _numbers;
}
set
{
_numbers = value;
OnPropertyChanged("Numbers");
}
}
public MainWindow()
{
InitializeComponent();
Numbers = new List<double>(){ 1.0, 2.0, 3.0};
comboBoxBinding = new Binding();
comboBoxBinding.Path = new PropertyPath("Numbers");
comboBoxBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, comboBoxBinding);
DataContext = this;
}
private void NewNumberClick(object sender, RoutedEventArgs e)
{
Random rand = new Random();
double newNumber = 2.0 - rand.NextDouble();
Numbers.Add(newNumber);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
c# wpf combobox binding
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I spend few hours on this working on bigger project, so I have made simple example. Problem is when you press "Add" button, it adds numbers to ComboBox item source property...Great, but when you open or select any item from comboBox, binding stops working.
I must be missing something.
XAML:
....
<Grid>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0"
VerticalAlignment="Top" Width="120"/>
<Button x:Name="AddButton" Content="Add" HorizontalAlignment="Left"
Margin="82,143,0,0" VerticalAlignment="Top" Width="75"
Click="NewNumberClick"/>
</Grid>
...
C# code:
namespace ComboBoxBinding
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<double> _numbers;
Binding comboBoxBinding;
public List<double> Numbers
{
get
{
return _numbers;
}
set
{
_numbers = value;
OnPropertyChanged("Numbers");
}
}
public MainWindow()
{
InitializeComponent();
Numbers = new List<double>(){ 1.0, 2.0, 3.0};
comboBoxBinding = new Binding();
comboBoxBinding.Path = new PropertyPath("Numbers");
comboBoxBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, comboBoxBinding);
DataContext = this;
}
private void NewNumberClick(object sender, RoutedEventArgs e)
{
Random rand = new Random();
double newNumber = 2.0 - rand.NextDouble();
Numbers.Add(newNumber);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
c# wpf combobox binding
New contributor
I spend few hours on this working on bigger project, so I have made simple example. Problem is when you press "Add" button, it adds numbers to ComboBox item source property...Great, but when you open or select any item from comboBox, binding stops working.
I must be missing something.
XAML:
....
<Grid>
<ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0"
VerticalAlignment="Top" Width="120"/>
<Button x:Name="AddButton" Content="Add" HorizontalAlignment="Left"
Margin="82,143,0,0" VerticalAlignment="Top" Width="75"
Click="NewNumberClick"/>
</Grid>
...
C# code:
namespace ComboBoxBinding
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
private List<double> _numbers;
Binding comboBoxBinding;
public List<double> Numbers
{
get
{
return _numbers;
}
set
{
_numbers = value;
OnPropertyChanged("Numbers");
}
}
public MainWindow()
{
InitializeComponent();
Numbers = new List<double>(){ 1.0, 2.0, 3.0};
comboBoxBinding = new Binding();
comboBoxBinding.Path = new PropertyPath("Numbers");
comboBoxBinding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, comboBoxBinding);
DataContext = this;
}
private void NewNumberClick(object sender, RoutedEventArgs e)
{
Random rand = new Random();
double newNumber = 2.0 - rand.NextDouble();
Numbers.Add(newNumber);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName = null)
{
if (PropertyChanged != null)
PropertyChanged.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
}
c# wpf combobox binding
c# wpf combobox binding
New contributor
New contributor
edited 1 hour ago
sirandy
1,35221623
1,35221623
New contributor
asked 2 hours ago
user3601308
1
1
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
your source is a List
, it won't notify the UI about member updates. You could use ObservableCollection
instead or call OnPropertyChanged
each time after you do .Add
More importantly you should use a real DataContext
instead of your UI
class and you should do the binding in xaml
not in code behind
add a comment |
up vote
0
down vote
Use ObservableCollection
instead of List. List don't provide a notification when somethings inside a list changes.
ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there. Code can add event handlers to see when the collection has changed and then react through the event handler to do some additional processing. This may be changing a UI or performing some other operation.
See: What is the use of ObservableCollection in .net?
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
your source is a List
, it won't notify the UI about member updates. You could use ObservableCollection
instead or call OnPropertyChanged
each time after you do .Add
More importantly you should use a real DataContext
instead of your UI
class and you should do the binding in xaml
not in code behind
add a comment |
up vote
0
down vote
your source is a List
, it won't notify the UI about member updates. You could use ObservableCollection
instead or call OnPropertyChanged
each time after you do .Add
More importantly you should use a real DataContext
instead of your UI
class and you should do the binding in xaml
not in code behind
add a comment |
up vote
0
down vote
up vote
0
down vote
your source is a List
, it won't notify the UI about member updates. You could use ObservableCollection
instead or call OnPropertyChanged
each time after you do .Add
More importantly you should use a real DataContext
instead of your UI
class and you should do the binding in xaml
not in code behind
your source is a List
, it won't notify the UI about member updates. You could use ObservableCollection
instead or call OnPropertyChanged
each time after you do .Add
More importantly you should use a real DataContext
instead of your UI
class and you should do the binding in xaml
not in code behind
answered 1 hour ago
Steve
7,20551542
7,20551542
add a comment |
add a comment |
up vote
0
down vote
Use ObservableCollection
instead of List. List don't provide a notification when somethings inside a list changes.
ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there. Code can add event handlers to see when the collection has changed and then react through the event handler to do some additional processing. This may be changing a UI or performing some other operation.
See: What is the use of ObservableCollection in .net?
add a comment |
up vote
0
down vote
Use ObservableCollection
instead of List. List don't provide a notification when somethings inside a list changes.
ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there. Code can add event handlers to see when the collection has changed and then react through the event handler to do some additional processing. This may be changing a UI or performing some other operation.
See: What is the use of ObservableCollection in .net?
add a comment |
up vote
0
down vote
up vote
0
down vote
Use ObservableCollection
instead of List. List don't provide a notification when somethings inside a list changes.
ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there. Code can add event handlers to see when the collection has changed and then react through the event handler to do some additional processing. This may be changing a UI or performing some other operation.
See: What is the use of ObservableCollection in .net?
Use ObservableCollection
instead of List. List don't provide a notification when somethings inside a list changes.
ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there. Code can add event handlers to see when the collection has changed and then react through the event handler to do some additional processing. This may be changing a UI or performing some other operation.
See: What is the use of ObservableCollection in .net?
edited 1 hour ago
answered 1 hour ago
Kevin Kouketsu
579
579
add a comment |
add a comment |
user3601308 is a new contributor. Be nice, and check out our Code of Conduct.
user3601308 is a new contributor. Be nice, and check out our Code of Conduct.
user3601308 is a new contributor. Be nice, and check out our Code of Conduct.
user3601308 is a new contributor. Be nice, and check out our Code of Conduct.
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53266064%2fcombobox-itemsourceproperty-losts-binding-when-item-selected%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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