Bind Dictionary to ItemsControl in C#/WPF

15,639

Solution 1

I solved it by using this line:

  <TextBox Text="{Binding Value, Mode=OneWay}"></TextBox>  

The code on http://www.dev102.com/2008/03/07/binding-a-wpf-control-to-a-dictionary/ doesn't seem to work.

Solution 2

Perhaps try binding directly to the values of the dictionary:

ItemsSource="{Binding Persons.Values}"

If I am understanding your XAML properly, each object in the dictionary has a field called "Text" to which you are trying to bind. If so and you make the above changes, you will need to change your DataTemplate as well:

<TextBox Text="{Binding Text}" />

See this article for more info. HTH.

Share:
15,639
TastyEquation60
Author by

TastyEquation60

Head of Product at divio.com, the company behind Aldryn and backers of django-CMS.

Updated on July 08, 2022

Comments

  • TastyEquation60
    TastyEquation60 almost 2 years

    I'm trying to bind KeyValuePair Elements from a Dictionary to a ItemsControl. My Dictionary has 15 Elements and the following code shows me 15 TextBoxes:

    <WrapPanel Name="PersonsWrapPanel" Grid.Row="0">
        <ItemsControl ItemsSource="{Binding Persons}" >
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal" Width="auto">
                    </WrapPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                        <TextBox Text="{Binding Value.Text}"></TextBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </WrapPanel>
    

    Unfortunately without any TextBox content (which would be Key or Value). Any ideas?