Home > other >  How I can bind a List<KeyValuePair<MyClass2,int>> inside ObservableCollection<MyClass
How I can bind a List<KeyValuePair<MyClass2,int>> inside ObservableCollection<MyClass

Time:03-31

I have a class Binding with List<KeyValuePair<BoardElement,int>>:

public class Binding
{
    public List<KeyValuePair<BoardElement, int>> Outputs { get; set; }
}

And class BoardElement contains property Name:

public class BoardElement
{
    private string Name { get; set; }
}

And I have ObservableCollection<Binding>.

Now, I want Binding Name <- BoardElement <- KeyValuePair <- Outputs.

I try somthing like this

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel  Orientation="Horizontal">
            <TextBlock Text="{Binding Outputs.Key.Name}" SelectedIndex="{Binding SelectedType}"  />
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

But it doesn't work, because it needs something in the path here Binding Outputs.?.Key.Name. It works only if I choose first element Binding Outputs[0].Key.Name, but I want to create a TextBlock for each KeyValuePair and bring a Name to them.

public ObservableCollection<Binding> B { get; set; }

public MainWindow()
{
    var a = new Binding();
    
    a.Input = new BoardElement("D1");
    a.Outputs = new List<KeyValuePair<BoardElement, int>>();

    var c = new BoardElement("D2");
    a.Outputs.Add(new KeyValuePair<BoardElement, int>(c, 7));
    
    B = new ObservableCollection<Binding>();
    B.Add(a);
    
    ListOfBindngs.ItemsSource = B;
}

So I want to know how Binding colletions in another collection? I can only choose by index ([0], [1], et cetera...) but I want to show them all

CodePudding user response:

You have a nested list: list of Binding and each of them have a list of KeyValuePair.

Option A, Nested ListBox:

<ListBox SelectedIndex="{Binding SelectedType}" x:Name="ListOfBindngs" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel  Orientation="Horizontal">
                <ListBox ItemsSource="{Binding outputs}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Key.Name}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Option B, Converter:

public class ConvertOutputs : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var list = (List<KeyValuePair<BoardElement, int>>)value;
        return string.Join(", ", list.Select(x => x.Key.Name));
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => null;
}

And in xaml:

<ListBox SelectedIndex="{Binding SelectedType}" x:Name="ListOfBindngs" >
    <ListBox.Resources>
        <local:ConvertOutputs x:Key="ConvertOutputs"/>
    </ListBox.Resources>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel  Orientation="Horizontal">
                <TextBlock Text="{Binding outputs, Converter={StaticResource ConvertOutputs}}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
  • Related