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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. WPF
  4. WPF Combobox databinding

WPF Combobox databinding

Scheduled Pinned Locked Moved WPF
wpfcsharphelpquestion
29 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.
  • U User 4032528

    I think that you are missing the ElementName in the ItemsSource Binding. In this case the ElementName is the x:Name of your window or UserControl. IsSynchronizedWithCurrentItem="True" TabIndex="10" Grid.Column="1" Grid.ColumnSpan="4" ItemsSource="{Binding Path=ParameterNameCollection, ElementName=UserControl, Mode=Default}" SelectedValue="{Binding SelectedParameter.Name}" Style="{DynamicResource BaseComboBox}">

    A Offline
    A Offline
    ABitSmart
    wrote on last edited by
    #4

    No, he is using DataContext of the parent implicitly.

    1 Reply Last reply
    0
    • A ABitSmart

      Did you try with Two-way binding ? Or else refer to this[^], Eslam has provided a sample with INotifyPropertyChanged.

      B Offline
      B Offline
      babongita
      wrote on last edited by
      #5

      I have tried TwoWay, but that didn't fix the problem, any other ideas?

      A 1 Reply Last reply
      0
      • B babongita

        I have a combobox which loads and displays exactly how I want it to, it is in a view and I am able to retrieve the pieces from this view without any problem. The problem I have is when I throw the object back at the view the combobox(which is bound to a collection of objects) the combobox doesn't show the information. What am I missing? Here is the combobox XAML:

        B Offline
        B Offline
        babongita
        wrote on last edited by
        #6

        on a side note this happens with all of the comboboxes, but I have them all coded the same so I have the same issue.

        1 Reply Last reply
        0
        • B babongita

          I have tried TwoWay, but that didn't fix the problem, any other ideas?

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #7

          Well, did you try the INotifyPropertyChanged suggestion from Eslam that I shared? Or this[^]

          B 1 Reply Last reply
          0
          • A ABitSmart

            Well, did you try the INotifyPropertyChanged suggestion from Eslam that I shared? Or this[^]

            B Offline
            B Offline
            babongita
            wrote on last edited by
            #8

            Yes the all of my base classes implement INotifyProperyChanged

            A 1 Reply Last reply
            0
            • B babongita

              Yes the all of my base classes implement INotifyProperyChanged

              A Offline
              A Offline
              ABitSmart
              wrote on last edited by
              #9

              Can you share how you are updating the object in code ? Try calling the property change notification explicitly using SendPropertyChanged("ParameterNameCollection").

              B 1 Reply Last reply
              0
              • A ABitSmart

                Can you share how you are updating the object in code ? Try calling the property change notification explicitly using SendPropertyChanged("ParameterNameCollection").

                B Offline
                B Offline
                babongita
                wrote on last edited by
                #10

                There are four different view models since there is are parent child relationships in this scenario. Here is an attempt to explain TabParameterViewModel has a DP of ParameterCollection (which is of Type ObservableCollection) has a DP of Parameter (bound to the selected item in a ListView) ParameterViewModel has a DP of Parameter which is set to a clone of TabParameterViewModel.Parameter has a DP of ParameterDefaultCollection (again an ObservableCollection) this.Parameter.Defaults ParameterDefaultsViewModel has a DP of ParameterDefaultCollection bound to a ListView has a DP of ParameterDefault bound to the selectedItem in the ListView ParameterDefaultViewModel has a DP of ParameterDefault which is set to a clone of ParameterDefaultsViewModel.ParameterDefault The Parameter Model has a Name property which is a class of ParameterName, which contains other attributes that are needed for the ParameterName. It also has a string property called ParameterLabel which I have bound to a textbox, when I change the SelectedItem in the listbox int TabParameterView the corresponding attributes change in the ParameterView, but any item bound to an object (in this case a Combobox) that attribute is not populated.

                A 2 Replies Last reply
                0
                • B babongita

                  There are four different view models since there is are parent child relationships in this scenario. Here is an attempt to explain TabParameterViewModel has a DP of ParameterCollection (which is of Type ObservableCollection) has a DP of Parameter (bound to the selected item in a ListView) ParameterViewModel has a DP of Parameter which is set to a clone of TabParameterViewModel.Parameter has a DP of ParameterDefaultCollection (again an ObservableCollection) this.Parameter.Defaults ParameterDefaultsViewModel has a DP of ParameterDefaultCollection bound to a ListView has a DP of ParameterDefault bound to the selectedItem in the ListView ParameterDefaultViewModel has a DP of ParameterDefault which is set to a clone of ParameterDefaultsViewModel.ParameterDefault The Parameter Model has a Name property which is a class of ParameterName, which contains other attributes that are needed for the ParameterName. It also has a string property called ParameterLabel which I have bound to a textbox, when I change the SelectedItem in the listbox int TabParameterView the corresponding attributes change in the ParameterView, but any item bound to an object (in this case a Combobox) that attribute is not populated.

                  A Offline
                  A Offline
                  ABitSmart
                  wrote on last edited by
                  #11

                  Your orignal post had the ComboBox bound to "ParameterNameCollection" which doesn't appear in your description. What collection is it ?

                  B 1 Reply Last reply
                  0
                  • A ABitSmart

                    Your orignal post had the ComboBox bound to "ParameterNameCollection" which doesn't appear in your description. What collection is it ?

                    B Offline
                    B Offline
                    babongita
                    wrote on last edited by
                    #12

                    The ParameterViewModel has an ObservableCollection ParameterNameCollection which is a collection of possible names that it could be, with attributes associated to that name, so the Collection is what fills the combobox, but I want the SelectedItem to be the ParameterName of the Parameter object. If I was to post the complete class for the ParameterViewModel it would look like this: ParameterViewModel has a ControlModeCollection (ObservableCollection) has a ControlTypeCollection (ObservableCollection) has a DataTypeCollection (ObservableCollection) has a ParameterMethodCollection (ObservableCollection) has a ParameterNameCollection (ObservableCollection) has a DP of SelectedParameter (of Type Parameter) has a DP of SelectedParameterDefaults (I think this is redundant since it is an ObservableCollection) Then the Parameter class looks like the following has a Name (of Type ParameterName) has a DataType (of Type DataType) has a ScreenLabel (of Type String) etc. etc. the TextBox XAML works as expected, the ComboBox doesn't

                    A 1 Reply Last reply
                    0
                    • B babongita

                      There are four different view models since there is are parent child relationships in this scenario. Here is an attempt to explain TabParameterViewModel has a DP of ParameterCollection (which is of Type ObservableCollection) has a DP of Parameter (bound to the selected item in a ListView) ParameterViewModel has a DP of Parameter which is set to a clone of TabParameterViewModel.Parameter has a DP of ParameterDefaultCollection (again an ObservableCollection) this.Parameter.Defaults ParameterDefaultsViewModel has a DP of ParameterDefaultCollection bound to a ListView has a DP of ParameterDefault bound to the selectedItem in the ListView ParameterDefaultViewModel has a DP of ParameterDefault which is set to a clone of ParameterDefaultsViewModel.ParameterDefault The Parameter Model has a Name property which is a class of ParameterName, which contains other attributes that are needed for the ParameterName. It also has a string property called ParameterLabel which I have bound to a textbox, when I change the SelectedItem in the listbox int TabParameterView the corresponding attributes change in the ParameterView, but any item bound to an object (in this case a Combobox) that attribute is not populated.

                      A Offline
                      A Offline
                      ABitSmart
                      wrote on last edited by
                      #13

                      babongita wrote:

                      but any item bound to an object (in this case a Combobox) that attribute is not populated

                      The above statement confuses me. It says any item bound to the object is not populated. You mean never or again just when you do something from code ?

                      B 1 Reply Last reply
                      0
                      • B babongita

                        The ParameterViewModel has an ObservableCollection ParameterNameCollection which is a collection of possible names that it could be, with attributes associated to that name, so the Collection is what fills the combobox, but I want the SelectedItem to be the ParameterName of the Parameter object. If I was to post the complete class for the ParameterViewModel it would look like this: ParameterViewModel has a ControlModeCollection (ObservableCollection) has a ControlTypeCollection (ObservableCollection) has a DataTypeCollection (ObservableCollection) has a ParameterMethodCollection (ObservableCollection) has a ParameterNameCollection (ObservableCollection) has a DP of SelectedParameter (of Type Parameter) has a DP of SelectedParameterDefaults (I think this is redundant since it is an ObservableCollection) Then the Parameter class looks like the following has a Name (of Type ParameterName) has a DataType (of Type DataType) has a ScreenLabel (of Type String) etc. etc. the TextBox XAML works as expected, the ComboBox doesn't

                        A Offline
                        A Offline
                        ABitSmart
                        wrote on last edited by
                        #14

                        babongita wrote:

                        SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"

                        and

                        babongita wrote:

                        <TextBlock Text="{Binding ParameterDisplayName}"/>

                        I guess both of these are bound to the Parameter object. Was just curious to know if these are two different properties or a typo ? Cause your textbox is bound to a property names "ScreenLabel", which I think is the display label.

                        babongita wrote:

                        Then the Parameter class looks like the following has a Name (of Type ParameterName)

                        In your description, you say, Name is of type "ParameterName". So then, I suspect this,

                        SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"

                        Here you are binding to SelectedParameter.Name where Name is of type ParamterName. The binding will fail since it is not binding to a string type. If you open your output window in the solution explorer (assuming you are using VS) you would be able to see binding failure messages logged.

                        B 1 Reply Last reply
                        0
                        • A ABitSmart

                          babongita wrote:

                          but any item bound to an object (in this case a Combobox) that attribute is not populated

                          The above statement confuses me. It says any item bound to the object is not populated. You mean never or again just when you do something from code ?

                          B Offline
                          B Offline
                          babongita
                          wrote on last edited by
                          #15

                          OK, There is a listbox below, which is bound to Collection of Parameters(this is a class, not what you would typically think of when you think parameters), then there is a "view" above it which shows the "detail" (attributes) of the SelectedItem in the listbox. All of the TextBoxes or the CheckBoxes show the "detail" (attributes) correctly. But when the "detail" (attribute) is an item in a Combobox the item doesn't show in the Combobox even though it is bound to that "detail"(attribute). The combobox is populated with all of the possible selections, but the SelectedItem, isn't shown in that combobox.

                          1 Reply Last reply
                          0
                          • A ABitSmart

                            babongita wrote:

                            SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"

                            and

                            babongita wrote:

                            <TextBlock Text="{Binding ParameterDisplayName}"/>

                            I guess both of these are bound to the Parameter object. Was just curious to know if these are two different properties or a typo ? Cause your textbox is bound to a property names "ScreenLabel", which I think is the display label.

                            babongita wrote:

                            Then the Parameter class looks like the following has a Name (of Type ParameterName)

                            In your description, you say, Name is of type "ParameterName". So then, I suspect this,

                            SelectedValue="{Binding SelectedParameter.Name, Mode=TwoWay}"

                            Here you are binding to SelectedParameter.Name where Name is of type ParamterName. The binding will fail since it is not binding to a string type. If you open your output window in the solution explorer (assuming you are using VS) you would be able to see binding failure messages logged.

                            B Offline
                            B Offline
                            babongita
                            wrote on last edited by
                            #16

                            First of all you are correct they are bound to different properties of the Parameter object. My question is though, that I am binding the ComboBox to a Collection of Classes, and the Parameter object as a property which is a the same class, so I would think that the SelectedValue should be able to bind directly to that Value, since they are of the same type. It is just instead of it being a string object, is of another type. I tried using: SelectedValue="{Binding SelectedParameter.Name.ParameterDisplayName, Mode=TwoWay}" but then the actual property doesn't get set correctly. How can I set that combobox to be the correct item then?

                            A 1 Reply Last reply
                            0
                            • B babongita

                              First of all you are correct they are bound to different properties of the Parameter object. My question is though, that I am binding the ComboBox to a Collection of Classes, and the Parameter object as a property which is a the same class, so I would think that the SelectedValue should be able to bind directly to that Value, since they are of the same type. It is just instead of it being a string object, is of another type. I tried using: SelectedValue="{Binding SelectedParameter.Name.ParameterDisplayName, Mode=TwoWay}" but then the actual property doesn't get set correctly. How can I set that combobox to be the correct item then?

                              A Offline
                              A Offline
                              ABitSmart
                              wrote on last edited by
                              #17

                              Give this a try,

                              <ComboBox x:Name="cboParameterName"
                              VerticalAlignment="Center"
                              IsSynchronizedWithCurrentItem="True"
                              TabIndex="10"
                              Grid.Column="1"
                              Grid.ColumnSpan="4"
                              ItemsSource="{Binding Path=ParameterNameCollection}"
                              SelectedValue="{Binding Path=SelectedParameter.Name.ParameterDisplayName}"
                              SelectedValuePath="Name.ParameterDisplayName"
                              Style="{DynamicResource BaseComboBox}">
                              <ComboBox.ItemTemplate>
                              <DataTemplate>
                              <TextBlock Text="{Binding Name.ParameterDisplayName}"/>
                              </DataTemplate>
                              </ComboBox.ItemTemplate>
                              </ComboBox>

                              Note: Added SelectedValuePath and changed the DataTemplate (fingers crossed ,X,, )

                              B 2 Replies Last reply
                              0
                              • A ABitSmart

                                Give this a try,

                                <ComboBox x:Name="cboParameterName"
                                VerticalAlignment="Center"
                                IsSynchronizedWithCurrentItem="True"
                                TabIndex="10"
                                Grid.Column="1"
                                Grid.ColumnSpan="4"
                                ItemsSource="{Binding Path=ParameterNameCollection}"
                                SelectedValue="{Binding Path=SelectedParameter.Name.ParameterDisplayName}"
                                SelectedValuePath="Name.ParameterDisplayName"
                                Style="{DynamicResource BaseComboBox}">
                                <ComboBox.ItemTemplate>
                                <DataTemplate>
                                <TextBlock Text="{Binding Name.ParameterDisplayName}"/>
                                </DataTemplate>
                                </ComboBox.ItemTemplate>
                                </ComboBox>

                                Note: Added SelectedValuePath and changed the DataTemplate (fingers crossed ,X,, )

                                B Offline
                                B Offline
                                babongita
                                wrote on last edited by
                                #18

                                I have been playing with using the SelectedIndex. And it seems to work, at least for now, the problem that I foresee is that I will have to go through the Combobox source to get the index each time, in the event that the Index would change, either by addition or substraction of Metadata. What are your thoughts on that?

                                1 Reply Last reply
                                0
                                • A ABitSmart

                                  Give this a try,

                                  <ComboBox x:Name="cboParameterName"
                                  VerticalAlignment="Center"
                                  IsSynchronizedWithCurrentItem="True"
                                  TabIndex="10"
                                  Grid.Column="1"
                                  Grid.ColumnSpan="4"
                                  ItemsSource="{Binding Path=ParameterNameCollection}"
                                  SelectedValue="{Binding Path=SelectedParameter.Name.ParameterDisplayName}"
                                  SelectedValuePath="Name.ParameterDisplayName"
                                  Style="{DynamicResource BaseComboBox}">
                                  <ComboBox.ItemTemplate>
                                  <DataTemplate>
                                  <TextBlock Text="{Binding Name.ParameterDisplayName}"/>
                                  </DataTemplate>
                                  </ComboBox.ItemTemplate>
                                  </ComboBox>

                                  Note: Added SelectedValuePath and changed the DataTemplate (fingers crossed ,X,, )

                                  B Offline
                                  B Offline
                                  babongita
                                  wrote on last edited by
                                  #19

                                  BTW, I tried your suggestion and the first problem was that the combobox wasn't showing what was loaded, the second problem was that the parameter.name wasn't being set either

                                  A 1 Reply Last reply
                                  0
                                  • B babongita

                                    BTW, I tried your suggestion and the first problem was that the combobox wasn't showing what was loaded, the second problem was that the parameter.name wasn't being set either

                                    A Offline
                                    A Offline
                                    ABitSmart
                                    wrote on last edited by
                                    #20

                                    babongita wrote:

                                    combobox wasn't showing what was loaded

                                    What did it show then ? Was there any value in the SelectedParameter, since that is what is driving the SelectedItem.

                                    babongita wrote:

                                    parameter.name wasn't being set either

                                    Why is this a combobox problem ?? Combobox is reading this value, someone has to be setting it ?? Did you check your output window? It would be logging DataBinding errors.

                                    B 1 Reply Last reply
                                    0
                                    • A ABitSmart

                                      babongita wrote:

                                      combobox wasn't showing what was loaded

                                      What did it show then ? Was there any value in the SelectedParameter, since that is what is driving the SelectedItem.

                                      babongita wrote:

                                      parameter.name wasn't being set either

                                      Why is this a combobox problem ?? Combobox is reading this value, someone has to be setting it ?? Did you check your output window? It would be logging DataBinding errors.

                                      B Offline
                                      B Offline
                                      babongita
                                      wrote on last edited by
                                      #21

                                      I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values. But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same I guess I could create a converter for that couldn't I, then it would be more of an "automated" thing, right?

                                      A 1 Reply Last reply
                                      0
                                      • B babongita

                                        I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values. But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same I guess I could create a converter for that couldn't I, then it would be more of an "automated" thing, right?

                                        A Offline
                                        A Offline
                                        ABitSmart
                                        wrote on last edited by
                                        #22

                                        babongita wrote:

                                        I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values.

                                        That is because some binding is failing. If you check the Output window it would clearly mention which binding is failing and why. The comboox SelectedItem is being driven by the SelectedParamter Dependency Property. Check what value it has. If it is null(or the Name property), then the combobox cannot do anything.

                                        babongita wrote:

                                        But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same

                                        So, if I understand you correct, you will search for the objects in the comboxbox and set the combobox SelectedIndex to it ? Would this be on the SelectionChange of your listviewm - where you would get the listview's selected item and search that in the combobox ?

                                        babongita wrote:

                                        I could create a converter

                                        Well, that seems to be a *workaround*. I would fix the damn databinding issue :) I think you could look at this as an option,

                                        <ComboBox x:Name="cboParameterName"
                                        VerticalAlignment="Center"
                                        IsSynchronizedWithCurrentItem="True"
                                        TabIndex="10"
                                        Grid.Column="1"
                                        Grid.ColumnSpan="4"
                                        ItemsSource="{Binding Path=ParameterNameCollection}"
                                        SelectedItem="{Binding Path=SelectedParameter}"
                                        Style="{DynamicResource BaseComboBox}"
                                        <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                        <TextBlock Text="{Binding Name.ParameterDisplayName}"/>
                                        </DataTemplate>
                                        </ComboBox.ItemTemplate>
                                        </ComboBox>

                                        One more thing is, since you are driving this combobox from a listview, why don't you bind the ListView's SelectedItem to the combobox's SelectedItem ,

                                        <ComboBox x:Name="cboParameterName"
                                        VerticalAlignment="Center"
                                        IsSynchronizedWithCurrentItem=

                                        B 1 Reply Last reply
                                        0
                                        • A ABitSmart

                                          babongita wrote:

                                          I didn't check the output window to see what was happening. When I hit the dropdown on the combo I couldn't see anything in it, but the itemsource had the values.

                                          That is because some binding is failing. If you check the Output window it would clearly mention which binding is failing and why. The comboox SelectedItem is being driven by the SelectedParamter Dependency Property. Check what value it has. If it is null(or the Name property), then the combobox cannot do anything.

                                          babongita wrote:

                                          But digging into the SelectedIndex seems to be the way to go here, it is performing as expected. But like I said, I will need to get the Index each time the screen loads, since the Index may change, not often but enough that I can't rely on that being the same

                                          So, if I understand you correct, you will search for the objects in the comboxbox and set the combobox SelectedIndex to it ? Would this be on the SelectionChange of your listviewm - where you would get the listview's selected item and search that in the combobox ?

                                          babongita wrote:

                                          I could create a converter

                                          Well, that seems to be a *workaround*. I would fix the damn databinding issue :) I think you could look at this as an option,

                                          <ComboBox x:Name="cboParameterName"
                                          VerticalAlignment="Center"
                                          IsSynchronizedWithCurrentItem="True"
                                          TabIndex="10"
                                          Grid.Column="1"
                                          Grid.ColumnSpan="4"
                                          ItemsSource="{Binding Path=ParameterNameCollection}"
                                          SelectedItem="{Binding Path=SelectedParameter}"
                                          Style="{DynamicResource BaseComboBox}"
                                          <ComboBox.ItemTemplate>
                                          <DataTemplate>
                                          <TextBlock Text="{Binding Name.ParameterDisplayName}"/>
                                          </DataTemplate>
                                          </ComboBox.ItemTemplate>
                                          </ComboBox>

                                          One more thing is, since you are driving this combobox from a listview, why don't you bind the ListView's SelectedItem to the combobox's SelectedItem ,

                                          <ComboBox x:Name="cboParameterName"
                                          VerticalAlignment="Center"
                                          IsSynchronizedWithCurrentItem=

                                          B Offline
                                          B Offline
                                          babongita
                                          wrote on last edited by
                                          #23

                                          I was thinking about binding to the ListView but the problem I had was that the user has the option to Add/Edit/Remove (all buttons on the view) a parameter, and I couldn't figure out how to add a new parameter and still have it be bound. Does that make sense? Can I email you offline? I would like to discuss this offline with some screen shots of what I am trying to duplicate.

                                          modified on Friday, March 6, 2009 12:46 PM

                                          A 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