Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Form with ComboBoxes bound to the same source.

Form with ComboBoxes bound to the same source.

Scheduled Pinned Locked Moved WPF
wpfcsharpwcfquestionannouncement
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cjb110
    wrote on last edited by
    #1

    Apologies if the question isn't clear, I'm struggling to find the right terminology. I have a entity that has properties which are the same entity i.e.:

    public class Unit
    {
    public int Id {get;set;}
    public string Name {get;set;}

    public int ParentUnitId {get;set;}
    

    }

    On my WPF form I'd like a combobox for the user to select which Unit is the ParentUnitId. So far I've gotten it by using a List, but this a) doesn't update if you add a new Unit, b) doesn't seem 'right', I feel like I should be able to achieve this with binding alone.

    If I just have my XAML starting the ComboBoxes ItemSources are the same source as the Form, then they act as filters/selectors and the whole form moves to that record. I'm using a CollectionViewSource as the DataContext for my Window.

    P C 2 Replies Last reply
    0
    • C cjb110

      Apologies if the question isn't clear, I'm struggling to find the right terminology. I have a entity that has properties which are the same entity i.e.:

      public class Unit
      {
      public int Id {get;set;}
      public string Name {get;set;}

      public int ParentUnitId {get;set;}
      

      }

      On my WPF form I'd like a combobox for the user to select which Unit is the ParentUnitId. So far I've gotten it by using a List, but this a) doesn't update if you add a new Unit, b) doesn't seem 'right', I feel like I should be able to achieve this with binding alone.

      If I just have my XAML starting the ComboBoxes ItemSources are the same source as the Form, then they act as filters/selectors and the whole form moves to that record. I'm using a CollectionViewSource as the DataContext for my Window.

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Don't use a List<Unit> as the collection behind your ItemsSource use ObservableCollection<Unit> as this raises change notifications when the number of items in the list changes. As far as the rest of your question goes, I'm really not clear what you're trying to achieve here.

      C 1 Reply Last reply
      0
      • P Pete OHanlon

        Don't use a List<Unit> as the collection behind your ItemsSource use ObservableCollection<Unit> as this raises change notifications when the number of items in the list changes. As far as the rest of your question goes, I'm really not clear what you're trying to achieve here.

        C Offline
        C Offline
        cjb110
        wrote on last edited by
        #3

        Yea I understand about the List / ObservableCollection bit. I'll have another go:) On my form, which allows you enter new units and change their details, one of the details is 'which is the parent unit'. This is a combobox, which should allow the user to select from the list of units. Its the correct setup of this combobox that I'm having problems with. I *think* I should be able to achieve this with pure XAML, but I'm not sure how. As I think the pure XAML solution will also have the advantage that if a new unit is added on the form, the combobox lists will all contain the new unit (as they're all the same source in some sense), which my current solution of a List wont have. And unless I've missed something, just changing List to ObservableCollection wont resolve that will it?

        P 1 Reply Last reply
        0
        • C cjb110

          Yea I understand about the List / ObservableCollection bit. I'll have another go:) On my form, which allows you enter new units and change their details, one of the details is 'which is the parent unit'. This is a combobox, which should allow the user to select from the list of units. Its the correct setup of this combobox that I'm having problems with. I *think* I should be able to achieve this with pure XAML, but I'm not sure how. As I think the pure XAML solution will also have the advantage that if a new unit is added on the form, the combobox lists will all contain the new unit (as they're all the same source in some sense), which my current solution of a List wont have. And unless I've missed something, just changing List to ObservableCollection wont resolve that will it?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          It would help if you could show us your ViewModel. That way I should be able to get a clearer idea. As it is, I can see 3 or 4 things that you could be asking for and I'd hate to send you up a blind alley. Oh, and if you need to add new items, you definitely need the OC. A list will not notify the ComboBox that there are new items.

          C 1 Reply Last reply
          0
          • P Pete OHanlon

            It would help if you could show us your ViewModel. That way I should be able to get a clearer idea. As it is, I can see 3 or 4 things that you could be asking for and I'd hate to send you up a blind alley. Oh, and if you need to add new items, you definitely need the OC. A list will not notify the ComboBox that there are new items.

            C Offline
            C Offline
            cjb110
            wrote on last edited by
            #5

            Ah, well right now we're not using VM. There's not a massive amount of experience, so we want to start slow and with what we're used to, so thats CodeBehind. I've pasted, the XAML, Code Behind and entity here (given the size didn't seem sensible to flood the forum) Pastie[^] So each Unit has three fields (BusinessAreaId, DivisionalUnitId, ParentUnitId), that also reference a Unit, so I would like 3 comboboxes each with a list of units inside, and ideally as you add units, then the combobox items also grow. My current ef entity load code is:

                        BackgroundWorker worker = new BackgroundWorker();
                        //the action to run
                        worker.DoWork += (s, ev) =>
                            {
                                this.Dispatcher.Invoke(new Action(() =>
                                    UIHelper.ProgressBarRun(true)));
            
                                context.Units.Load();
                                context.UnitTypes.Load();
                                unitNames = new ObservableCollection(
                                    context.Units.Local.Select(f => new UnitName() { Id = f.Id, Name = f.Name }));
                            };
                        //what to run when the above completes
                        worker.RunWorkerCompleted += (s, ev) =>
                            {
                                viewSource.Source = context.Units.Local;
                                unitTypeIdComboBox.ItemsSource = context.UnitTypes.Local;
            
                                businessAreaIdComboBox.ItemsSource = unitNames;
                                divisionalUnitIdComboBox.ItemsSource = unitNames;
                                parentUnitIdComboBox.ItemsSource = unitNames;
            
                                this.Dispatcher.Invoke(new Action(() =>
                                    {
                                        UIHelper.ProgressBarRun(false);
                                        navFirstButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
                                    }));
                            };
                        //start the background work
                        worker.RunWorkerAsync();
            
            1 Reply Last reply
            0
            • C cjb110

              Apologies if the question isn't clear, I'm struggling to find the right terminology. I have a entity that has properties which are the same entity i.e.:

              public class Unit
              {
              public int Id {get;set;}
              public string Name {get;set;}

              public int ParentUnitId {get;set;}
              

              }

              On my WPF form I'd like a combobox for the user to select which Unit is the ParentUnitId. So far I've gotten it by using a List, but this a) doesn't update if you add a new Unit, b) doesn't seem 'right', I feel like I should be able to achieve this with binding alone.

              If I just have my XAML starting the ComboBoxes ItemSources are the same source as the Form, then they act as filters/selectors and the whole form moves to that record. I'm using a CollectionViewSource as the DataContext for my Window.

              C Offline
              C Offline
              cjb110
              wrote on last edited by
              #6

              Resolved it, adding design time data helped...though weirdly with it working the design time data now looks wrong. I'm not 100% this is fully correct, or the best or simplest way of doing this...but it does seem to work so far. I've got a few CollectionViewSources, but the first and last are the only two relevant to this.

              I needed the CollectionViewSource unitNameViewSource, so that I could set the ItemsSource on the ComboBox to something. If I didn't set it, or set it to {Binding} I got false values on the first record. The ComboBoxs are defined as:

              The other thing I think was important was to bind to BusinessArea and not the BusinessAreaId column. Let EF faff with translating that! My code behind is then:

                  private void Page\_Loaded(object sender, RoutedEventArgs e)
                  {
              
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups