Ask a questionAsk a question
 

AnswerWPF ComboBox Binding

  • Tuesday, November 03, 2009 7:08 PMj-a-h Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am trying to make my combobox's work like in access.  I am using a stackpanel to display my columns in my combobox.  Here is my code and then I'll tell you my problems:

    <ComboBox IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding COMPANY_, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Canvas.Left="117" Canvas.Top="39" Name="COMPANY_" VerticalAlignment="Top" Width="155">
    <StackPanel Orientation="Horizontal">
    <TextBlock Text="{Binding COMPANY_, Mode=TwoWay}" />
    <TextBlock Text="{Binding NAME1, Mode=TwoWay}" />
    <TextBlock Text="{Binding NAME2, Mode=TwoWay}" />
    <TextBlock Text="{Binding STREET, Mode=TwoWay}" />
    <TextBlock Text="{Binding CITY, Mode=TwoWay}" />
    <TextBlock Text="{Binding STATE, Mode=TwoWay}" />
    <TextBlock Text="{Binding ZIP5, Mode=TwoWay}" />
    </StackPanel>
    </ComboBox>

    'Call stored procedure to fill office drop down list

    COMPANY_.Items.Clear()

     

    COMPANY_.ItemsSource =

     

    From comp In dc.OI_CLM_WC_Claim_Main_code_COMPANY_Enter(My.Settings.Examiner.CP_COMPANY_) _
    Select comp.COMPANY_, comp.NAME1, comp.NAME2, _
    comp.STREET, comp.CITY, comp.STATE, comp.ZIP5
    Catch ex As InvalidOperationException
    End Try
    Try

    dc is my linq to sql datacontext.  The problem that I am having is that in the combobox it displays information like follows:

    { COMPANY_ = ABC, NAME1 = TEST, NAME2 = TEST, STREET = TEST, CITY = TEST, STATE = TEST, ZIP5 = TEST }

    I don't want to display the curly brackets and I don't want to display COMPANY_ =, I just want to display ABC.  The other problem is when I called a stored procedure from datacontext and set the page's datacontext to the results the selecteditem binding does not work on the combobox.  If anyone could help it would be greatly appreciated.

Answers

  • Thursday, November 05, 2009 2:37 PMj-a-h Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I got it figured out.  All I had to to was set the SelectedValuePath of the combobox to COMPANY_.  Thank you everyone for your help.

    • Marked As Answer byj-a-h Thursday, November 05, 2009 2:37 PM
    •  

All Replies

  • Wednesday, November 04, 2009 9:07 AMGeert van Horrik Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer

    If you want to put a StackPanel inside the ComboBox, you need to change its template, not just add a StackPanel as the content. Take a look at this article.


    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    • Proposed As Answer byRahul P Nath Wednesday, November 04, 2009 9:08 AM
    •  
  • Wednesday, November 04, 2009 9:30 AMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Whatever you have written will go into the Itemtemplate for the combobox .

    See this link

    Hope it helps
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
  • Wednesday, November 04, 2009 2:18 PMj-a-h Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Here is my updated code.  The only problem I am having now is that I want the SelectedValue of the Combo box tho bind to the first textblock's value in the stackpanel:

    <ComboBox IsSynchronizedWithCurrentItem="True" SelectedValue="{Binding COMPANY_, Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Canvas.Left="117" Canvas.Top="39" Name="COMPANY_" VerticalAlignment="Top" Width="155">
                                    <ComboBox.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding COMPANY_}" Width="50" />
                                                <TextBlock Text="{Binding NAME1}" Width="225" />
                                                <TextBlock Text="{Binding STREET}" Width="225" />
                                                <TextBlock Text="{Binding CITY}" Width="100" />
                                                <TextBlock Text="{Binding STATE}" Width="25" />
                                                <TextBlock Text="{Binding ZIP5}" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </ComboBox.ItemTemplate>
                                </ComboBox> 
    
  • Thursday, November 05, 2009 3:56 AMRahul P Nath Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    Since you have binded it to the poperty's value it will already the value for the selected item only.
    Since the textblock is also a part of the template it will keep changing as  the selection changes.
    Not quite getting what you want?Please make it more clear

    Thank You
    Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem
  • Thursday, November 05, 2009 1:42 PMj-a-h Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    O.k. I have a page with a bunch of textbox's and combobox's on them.  Someone searches for a person by last and when the user selects which person they want I call one of my stored procedures, using LINQ to SQL, to get all the information for that person.  I have binding on each and every textbox and combobox so when the ISingleResult is returned from the stored procedure I set the DataContext of the page equal to the ISingleResult.  Everything works except for the combobox's which are just displayed as blank (nothing selected).  I think the problem is because I'm trying set the SelectedValue based on the first column in the combobox and with multiple columns in the combobox the SelectedValue is actually an array of values.  I'm trying to make this like Microsoft Access where you can display multiple columns but bind the SelectedValue only to the first column.
  • Thursday, November 05, 2009 2:37 PMj-a-h Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I got it figured out.  All I had to to was set the SelectedValuePath of the combobox to COMPANY_.  Thank you everyone for your help.

    • Marked As Answer byj-a-h Thursday, November 05, 2009 2:37 PM
    •