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. WPF combobox selecteditem issue

WPF combobox selecteditem issue

Scheduled Pinned Locked Moved WPF
csharpdatabasewpfhelptutorial
8 Posts 3 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.
  • P Offline
    P Offline
    PretzelB
    wrote on last edited by
    #1

    I had a working example of a combobox jumping to the correct selecteditem based on the choice of another combobox but I can't seem to make it work with a very simple example. I'll try to keep the code to a minimum but I don't see why a choice in "product" will not cause the correct "category" to be selected. This same code seemed to work fine when working with an EF data model based on the NW database. Main window

    I S 2 Replies Last reply
    0
    • P PretzelB

      I had a working example of a combobox jumping to the correct selecteditem based on the choice of another combobox but I can't seem to make it work with a very simple example. I'll try to keep the code to a minimum but I don't see why a choice in "product" will not cause the correct "category" to be selected. This same code seemed to work fine when working with an EF data model based on the NW database. Main window

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      Is the product actually changing in the ViewModel? Set a breakpoint and make sure. If not, you might want to add Mode=TwoWay,UpdateSourceTrigger=PropertyChanged to the SelectedItem bindings. And on a related note, UpdateSourceTrigger=PropertyChanged is completely irrelevant on a OneWay binding, because the source never gets updated. Binding modes: * OneTime = Only changes when the DataContext is changed * OneWay = This property updates from the source, but changes aren't sent back * TwoWay = Updates in both directions Unless you specifically mark the property as BindsTwoWayByDefault, you should assume it's OneWay by default.

      Proud to have finally moved to the A-Ark. Which one are you in?
      Author of the Guardians Saga (Sci-Fi/Fantasy novels)

      P 1 Reply Last reply
      0
      • I Ian Shlasko

        Is the product actually changing in the ViewModel? Set a breakpoint and make sure. If not, you might want to add Mode=TwoWay,UpdateSourceTrigger=PropertyChanged to the SelectedItem bindings. And on a related note, UpdateSourceTrigger=PropertyChanged is completely irrelevant on a OneWay binding, because the source never gets updated. Binding modes: * OneTime = Only changes when the DataContext is changed * OneWay = This property updates from the source, but changes aren't sent back * TwoWay = Updates in both directions Unless you specifically mark the property as BindsTwoWayByDefault, you should assume it's OneWay by default.

        Proud to have finally moved to the A-Ark. Which one are you in?
        Author of the Guardians Saga (Sci-Fi/Fantasy novels)

        P Offline
        P Offline
        PretzelB
        wrote on last edited by
        #3

        The product is changing. I added the textblocks in the 2nd and 3rd grid rows to show the values of the properties as the combobox selection is made. I also put a break in and "mySelectedCategory" is updated when a choice is made to product. I made a change to TwoWay and saw no difference. My understanding of the binding modes matches what you have listed but I thought that UpdateSourceTrigger was still needed to let the viewmodel know that an update was made from the source. With OneWay I do indeed want to say that updates are not sent back, but UpdateSourceTrigger is there to let the view know that updates were made to the model.

        I 1 Reply Last reply
        0
        • P PretzelB

          I had a working example of a combobox jumping to the correct selecteditem based on the choice of another combobox but I can't seem to make it work with a very simple example. I'll try to keep the code to a minimum but I don't see why a choice in "product" will not cause the correct "category" to be selected. This same code seemed to work fine when working with an EF data model based on the NW database. Main window

          S Offline
          S Offline
          Simon Bang Terkildsen
          wrote on last edited by
          #4

          Have a look at this article Debugging WPF data bindings[^] Hopefully it can help you debug your binding. A quick tip, as myCategory does not override equals then mySelectedCategory will only be displayed/selected in the combobox if myCategoryList contain the very same reference.

          My number one dev tool? Google

          P 1 Reply Last reply
          0
          • P PretzelB

            The product is changing. I added the textblocks in the 2nd and 3rd grid rows to show the values of the properties as the combobox selection is made. I also put a break in and "mySelectedCategory" is updated when a choice is made to product. I made a change to TwoWay and saw no difference. My understanding of the binding modes matches what you have listed but I thought that UpdateSourceTrigger was still needed to let the viewmodel know that an update was made from the source. With OneWay I do indeed want to say that updates are not sent back, but UpdateSourceTrigger is there to let the view know that updates were made to the model.

            I Offline
            I Offline
            Ian Shlasko
            wrote on last edited by
            #5

            Ah, Simon nailed the problem... The category being dumped into "mySelectedCategory" isn't IN the list. It's an object created in GetMyCategory() that has the same values as one of the items in the list. So the solution is to override the Equals() function of the category, so it returns true when the ID and Name match (Or just the ID). You'll also be told to override GetHashCode (Also a good idea)... That way, the data binding system will be able to match "mySelectedCategory" to an item in the list, and select it for you.

            Proud to have finally moved to the A-Ark. Which one are you in?
            Author of the Guardians Saga (Sci-Fi/Fantasy novels)

            P 1 Reply Last reply
            0
            • S Simon Bang Terkildsen

              Have a look at this article Debugging WPF data bindings[^] Hopefully it can help you debug your binding. A quick tip, as myCategory does not override equals then mySelectedCategory will only be displayed/selected in the combobox if myCategoryList contain the very same reference.

              My number one dev tool? Google

              P Offline
              P Offline
              PretzelB
              wrote on last edited by
              #6

              In a previous version I had tried that but it appears I coded it incorrectly. For some reason I tried to implement IEqualityComparer and when I did nothing changed. Instead I tried an override to Equals and it seems to have worked. Sigh. I had the answer from a Google search a while back, I just implemented it incorrectly. Here's what I coded that appears to have worked.

              Public Overrides Function Equals(obj As Object) As Boolean
                  If IsNothing(obj) OrElse Not \[GetType\]().Equals(obj.GetType()) Then
                      Return False
                  End If
                  Dim myc As myCategory = CType(obj, myCategory)
                  Return CategoryID = myc.CategoryID
              End Function
              Public Overrides Function GetHashCode() As Integer
                  Return CategoryID.GetHashCode ^ CategoryName.GetHashCode
              End Function
              
              1 Reply Last reply
              0
              • I Ian Shlasko

                Ah, Simon nailed the problem... The category being dumped into "mySelectedCategory" isn't IN the list. It's an object created in GetMyCategory() that has the same values as one of the items in the list. So the solution is to override the Equals() function of the category, so it returns true when the ID and Name match (Or just the ID). You'll also be told to override GetHashCode (Also a good idea)... That way, the data binding system will be able to match "mySelectedCategory" to an item in the list, and select it for you.

                Proud to have finally moved to the A-Ark. Which one are you in?
                Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                P Offline
                P Offline
                PretzelB
                wrote on last edited by
                #7

                You were correct and I posted my solution in the other post. I think I see the issue. Regarding the statement that the category wasn't "in the list", given the common example displayed here, am I missing a better way to do this? My assumption is your data layer will have functions to get a list of objects (for maybe a combobox) and also have a function to grab an object by ID. Are you implying that maybe my data layer should be doing something different for it's "get by id" type function? I'm all for learning a better method.

                I 1 Reply Last reply
                0
                • P PretzelB

                  You were correct and I posted my solution in the other post. I think I see the issue. Regarding the statement that the category wasn't "in the list", given the common example displayed here, am I missing a better way to do this? My assumption is your data layer will have functions to get a list of objects (for maybe a combobox) and also have a function to grab an object by ID. Are you implying that maybe my data layer should be doing something different for it's "get by id" type function? I'm all for learning a better method.

                  I Offline
                  I Offline
                  Ian Shlasko
                  wrote on last edited by
                  #8

                  Well the trick is that you're not returning an item from the list of categories... You're returning an entirely different item with the same properties, when you do this:

                  Public Function GetMyCategory(ByVal id As Int32) As myCategory
                  Dim results = From c In _mycateorylist
                  Where c.CategoryID = id
                  Select c
                  Dim rs As IEnumerable(Of myCategory) = From x In results.AsEnumerable
                  Select New myCategory With
                  {
                  .CategoryID = x.CategoryID,
                  .CategoryName = x.CategoryName
                  }

                  Return rs.FirstOrDefault
                  End Function

                  If you just return results.FirstOrDefault, it'll give you the first result from the actual list. You're also doing the same thing with GetMyCategories(), by calling it once in the data model's constructor, and again in the view model's constructor, yielding two different collections with identical contents. The idea is to have one collection in the data model, give the view model a reference to that collection, and let the GUI bind to the view model's reference, so they all point to the same data.

                  Proud to have finally moved to the A-Ark. Which one are you in?
                  Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                  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