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));
}
}
}









share|improve this question









New contributor




user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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));
    }
    }
    }









    share|improve this question









    New contributor




    user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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));
      }
      }
      }









      share|improve this question









      New contributor




      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago









      sirandy

      1,35221623




      1,35221623






      New contributor




      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      user3601308

      1




      1




      New contributor




      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      user3601308 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.
























          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






          share|improve this answer




























            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?






            share|improve this answer























              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
              });


              }
              });






              user3601308 is a new contributor. Be nice, and check out our Code of Conduct.










               

              draft saved


              draft discarded


















              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
































              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






              share|improve this answer

























                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






                share|improve this answer























                  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






                  share|improve this answer












                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  Steve

                  7,20551542




                  7,20551542
























                      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?






                      share|improve this answer



























                        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?






                        share|improve this answer

























                          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?






                          share|improve this answer














                          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?







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited 1 hour ago

























                          answered 1 hour ago









                          Kevin Kouketsu

                          579




                          579






















                              user3601308 is a new contributor. Be nice, and check out our Code of Conduct.










                               

                              draft saved


                              draft discarded


















                              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.















                               


                              draft saved


                              draft discarded














                              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




















































































                              Popular posts from this blog

                              mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                              How to change which sound is reproduced for terminal bell?

                              Can I use Tabulator js library in my java Spring + Thymeleaf project?