0

How can I bind this tuple as value of a dictionary?

C#:

private Dictionary<int, (string, List<int>)> myDict;
public Dictionary<int, (string, List<int>)> MyDict
{
    get { return myDict; }
    set { myDict = value; }
}

XAML:

<ItemsControl ItemsSource="{Binding MyDict}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Vertical"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>

            <StackPanel Orientation="Vertical">

                <TextBlock Text="{Binding Key}"/>

                <ItemsControl ItemsSource="{Binding Value}">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>

                    <ItemsControl.ItemTemplate>
                        <DataTemplate>

                            <StackPanel Orientation="Vertical">
                                <TextBlock Text="{Binding Value.Item1}"/>
                            </StackPanel>

                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>

            </StackPanel>

        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

This binding is not working <TextBlock Text="{Binding Value.Item1}"/>. I have tried <TextBlock Text="{Binding Value[Item1]}"/> and <TextBlock Text="{Binding Value[0]}"/> but it is not working. I need a tuple there. When I try <TextBlock Text="{Binding Value}"/> it writes out the whole object content and name, what I dont want. Can you help me please?

UPDATE / SOLUTION: I have solved it. By writing (string, List<int>) I declare a ValueTuple and it is a struct. All I need to do is to declare it like a real Tuple type like this: Tuple<string, List<int>>

XAML: <TextBlock Text="{Binding Value.Item1}"/>

Now it is working.

juraj
  • 37
  • 6

0 Answers0