1

I have a combobox which hosts a textblock child element. I want to bind the textblock inside the combobox to a property called ResultList. I tried the code below, but it doesn't work. What have I missed out?

    <ComboBox x:Name="Test" HorizontalAlignment="Left" Margin="79,42,0,0" VerticalAlignment="Top" Width="344" 
              IsEditable="True">
        <ComboBox.Resources>
            <system:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</system:Double>
        </ComboBox.Resources>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}" >
                <Setter Property="Background" Value="#FFFFFF"/>
                <Setter Property="BorderThickness" Value="0" />
            </Style>
        </ComboBox.ItemContainerStyle>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=ResultList, Mode=OneWay}" DataContext="{Binding Path=ResultList, Mode=OneWay}" />
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
SeyedPooya Soofbaf
  • 2,654
  • 2
  • 29
  • 31
friartuck
  • 2,954
  • 4
  • 33
  • 67
  • 1
    where is the `ItemsSource` in you `comboBox` ? I guess you will like to bind `ResultList` to `ItemsSource` instead. – Bolu Sep 16 '13 at 09:55
  • What object has the `ResultList` property? How is the `DataContext` and/or `ItemsSource` set on the `ComboBox`? – Sheridan Sep 16 '13 at 09:58
  • Yes I can bind to ItemSource, and that works, but how do I then bind this my textblock? – friartuck Sep 16 '13 at 10:02
  • I assume by its name that `ResultList` is a list of items, so binding it to a text block doesn't make much sense. If what you are trying to do is display each of the items in the comboBox then like other have pointed out you should bind the `ItemsSource` of your `ComboBox`. In Addition, If you are simply just presenting it as plain text, comboBox does it for you and there is no need to use `DataTemplate` ... – Omri Btian Sep 16 '13 at 10:03
  • @Prof, do you mean when bind `ItemsSource` to your `comboBox` you don't know what for the `textblock` to bind with? just use `` – Bolu Sep 16 '13 at 10:09
  • Yes, I ResultList is a list of items, so I need to bind my combobox itemsource to ResultList, then bind my Textblock to the comboboxes's ItemSource. I tried But that didn't work. Please explain why? – friartuck Sep 16 '13 at 10:11
  • Woo worked – friartuck Sep 16 '13 at 10:13
  • Correct, that is what I mean... – Bolu Sep 16 '13 at 10:15

2 Answers2

1

So, to sum all the comments up:

You need to bind the list to ItemsSource of comboBox.

<ComboBox x:Name="Test" ItemsSrouce="{Binding ResultList}" ....>

And set TextBlock in the ItemTemplate to something like:

<TextBlock Text="{Binding Path=Age}" ..../> 
<TextBlock Text="{Binding Path=Name}" ..../> 
Bolu
  • 8,696
  • 4
  • 38
  • 70
0

You can't set both the DataContext and the Text property to the same value:

"{Binding Path=ResultList, Mode=OneWay}" 

You can try this:

<TextBlock Text="{Binding, Mode=OneWay}" DataContext="{Binding Path=ResultList}" />

But this might be better:

<TextBlock Text="{Binding Path=ResultList, Mode=OneWay}" />

Of course, it is hard to answer when you haven't provided all of the necessary information, such as what has been asked in the comments.

Sheridan
  • 68,826
  • 24
  • 143
  • 183