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 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.
  • 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
            • B babongita

              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 Offline
              A Offline
              ABitSmart
              wrote on last edited by
              #24

              I do not think it is a good idea to share yur email id in public.

              B 1 Reply Last reply
              0
              • A ABitSmart

                I do not think it is a good idea to share yur email id in public.

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

                OK well I modified that post. But is it possible that we could continue this offline?

                A 1 Reply Last reply
                0
                • B babongita

                  OK well I modified that post. But is it possible that we could continue this offline?

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

                  I am a dedicated member, always available at CP :)

                  B 2 Replies Last reply
                  0
                  • A ABitSmart

                    I am a dedicated member, always available at CP :)

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

                    OK I am going back through now and trying you recommendations and this is the error that I am getting in the Output window System.Windows.Data Error: 39 : BindingExpression path error: 'Name' property not found on 'object' ''ParameterName' (HashCode=62178992)'. BindingExpression:Path=Name.ParameterDisplayName; DataItem='ParameterName' (HashCode=62178992); target element is 'ComboBox' (Name='cboParameterName'); target property is 'NoTarget' (type 'Object')

                    1 Reply Last reply
                    0
                    • A ABitSmart

                      I am a dedicated member, always available at CP :)

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

                      OK I have finally figured it out, but it really is a different work around. Or at least it seems like it. If instead of creating the ParameterName objects with all of the properties associated with it, if I only create a collection of strings and then bind them to the dropdown box then I can make the binding work. But it is not the object itself it is only one piece of the object.

                      A 1 Reply Last reply
                      0
                      • B babongita

                        OK I have finally figured it out, but it really is a different work around. Or at least it seems like it. If instead of creating the ParameterName objects with all of the properties associated with it, if I only create a collection of strings and then bind them to the dropdown box then I can make the binding work. But it is not the object itself it is only one piece of the object.

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

                        I am suprised it does not work while binding to the the string property of the object. Well, if it is so, you can use a converter taking the object and returning a string for it rather than having to have a string collection. Which particular binding was failing, the one in the combobox DataTemplate ? I still feel that your data was not being populated correct.

                        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