Search code examples
wpfbindingcomboboxtextblock

WPF combobox two way binding with TextBlock


I created the following list

List<cb> combolist = new List<cb>();

combolist.Add(new cb() { name = "Neo",bloodgroup = "O+ve"}); 

combolist.Add(new cb() { name = "meo", bloodgroup = "O" });

combolist.Add(new cb() { name = "bsv", bloodgroup = "B+ve" });

cboxnames.ItemsSource = combolist;

Now I am creating a combobox that gets data from the above list using Item template

<ComboBox Margin="12,31,421,258" Name="cboxnames" IsEditable="False">
  <ComboBox.ItemTemplate>
     <DataTemplate>
             <TextBlock Text="{Binding name}"/>
         </DataTemplate>
        </ComboBox.ItemTemplate>   
</ComboBox>

Now I am creating an additional textblock that displays the item that is selected in the combobox

<TextBlock Height="28" HorizontalAlignment="Left" Background="LightGray" Margin="0,138,0,0" Text="{Binding UpdateSourceTrigger=PropertyChanged,ElementName=cboxnames,Mode=TwoWay,Path=SelectedItem.Content}" VerticalAlignment="Top" Width="191" />

The problem is whenever I am selecting an item from combobox, that item is not displayed in the textblock, please help!!!


Solution

  • <TextBlock Text="{Binding UpdateSourceTrigger=PropertyChanged,
                              ElementName=cboxnames,
                              Mode=TwoWay,Path=SelectedItem.name}"/>
    

    use this..hope it helps you