Poser une questionPoser une question
 

TraitéeWPF ComboBox Binding

  • mardi 3 novembre 2009 19:08j-a-h Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.

Réponses

  • jeudi 5 novembre 2009 14:37j-a-h Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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.

    • Marqué comme réponsej-a-h jeudi 5 novembre 2009 14:37
    •  

Toutes les réponses

  • mercredi 4 novembre 2009 09:07Geert van Horrik Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Réponse proposée

    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!
    • Proposé comme réponseRahul P Nath mercredi 4 novembre 2009 09:08
    •  
  • mercredi 4 novembre 2009 09:30Rahul P Nath Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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
  • mercredi 4 novembre 2009 14:18j-a-h Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du 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> 
    
  • jeudi 5 novembre 2009 03:56Rahul P Nath Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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
  • jeudi 5 novembre 2009 13:42j-a-h Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    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.
  • jeudi 5 novembre 2009 14:37j-a-h Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     Traitée

    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.

    • Marqué comme réponsej-a-h jeudi 5 novembre 2009 14:37
    •