Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a ComboxBox, whose XAML is below. I need to bind textblocks that are elsewhere on the form to the textblocks in the ComboBox selected item. How do I do that?

HTML
<combobox x:name="VictimNames" margin="0,5,0,0" height="30" fontweight="Bold" width="600" 
="" itemssource="{Binding Form104CModel.Incident.Victims}" isreadonly="True" iseditable="False" selecteditem="">
    <combobox.itemtemplate>
        <datatemplate>
            <grid>
                <grid.rowdefinitions>
                    <rowdefinition>
                
                <grid.columndefinitions>
                    <columndefinition width="100">
                    <columndefinition width="100">
                    <columndefinition width="90">
                    <columndefinition width="100">
                    <columndefinition width="100">
                    <columndefinition width="100">
                
                <textblock grid.row="0" grid.column="0" x:name="VictimLastName" text="{Binding LastName}">
                <textblock grid.row="0" grid.column="1" x:name="VictimFirstName" text="{Binding FirstName}">
                <textblock grid.row="0" grid.column="2" x:name="VictimMiddleName" text="{Binding MiddleName}">
                <textblock grid.row="0" grid.column="3" x:name="VictimRace" text="{Binding Race}">
                <textblock grid.row="0" grid.column="4" x:name="VictimGender" text="{Binding Gender}">
                <textblock grid.row="0" grid.column="5" x:name="VictimDOB">
                    <textblock.text>
                        <binding path="DOB">
                            <binding.converter>
                                <infraconverters:dateconverter>
                                           </Binding.Converter>
                                        </Binding>
                                    </TextBlock.Text>   
                                    </TextBlock>
                                </Grid>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>


What I have tried:

I have tried
HTML
Text="{Binding VictimNames.SelectedItem.<field>}"

for each TextBlock, replacing <field> with the ComboBox Template TextBox x:Name, but that didn't work.
Posted
Updated 29-Mar-19 11:23am
v3
Comments
Member 12061698 29-Mar-19 13:52pm    
I asked for an answer to a problem, not an editorial opinion. If that's the best you can do, you're not helpful at all. And you're supposed to be a top expert? That kind of 'expert' help is the kind I don't need.

That's what happens when you chuck code-behind: confusion and delays.

You should bind the selected item (object) to a proper "type"; then bind that instance to the text boxes; with defaults if the Selected Index is -1 (i.e. "nothing" selected).

(Yes; I give opinions too).
 
Share this answer
 
v2
I found the answer.  It was as simple as I first thought.  The ComboBox was to be populated with victim data when the user selected a victim.  

I figured it had to be a simple binding to the selected item, and it is.  Here is how it is done for the InvolvementNumber, which is a victim property. VictimNames is the ComboBox, and the Path got me there. 

 <StackPanel Grid.Row="0" Grid.Column="0">
     <TextBlock> #</TextBlock>
     <TextBlock Name="VictimNumber"  
         Style="{StaticResource PartIIGridIsEnabledTextBlock}" 
         Text="{Binding ElementName=VictimNames,Path=SelectedItem.InvolvementNumber}"/> </StackPanel>

One more thing: The Path must go to the property of the underlying data, not the name of the object in the ComboBox.ItemTemplate.
 
Share this answer
 
v2
if I understand this correctly, when the user selects an item from the ComboBox, the selected item is used to populate an object which is bound to the TextBlocks.

How about this:
Use the select index of the ComboBox to bind the TextBlocks to the underlying data of the ComboBox.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900