Home > Mobile >  WPF Binding Listbox into ListView (different Source / datacontext)
WPF Binding Listbox into ListView (different Source / datacontext)

Time:03-21

i have these two classes.

classes:

public class OuterList
{
    public string Process { get; set; }

    public OuterList(string _process)
    {
        Process = _process;
    }
}

public class InnerList
{
    public string Order { get; set; }
    public string Product { get; set; }

    public InnerList(string _order, string _product)
    {
        Order = _order;
        Product = _product;
    }
}

And i will get a Listbox in a ListView.

XAML:

<ListView x:Name="Container" ItemsSource="{Binding OuterList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Margin="5" FontSize="24" FontFamily="Arial" Background="Red" Text="{Binding Process}"/>

                <ListBox x:Name="Orders" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=DataContext.InnerList}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Product}"/>
                                <TextBlock Text="{Binding Order}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Code:

public partial class MainWindow : Window
{
        ObservableCollection<OuterList> myContainers = new();
        ObservableCollection<InnerList> myItems = new();

    public MainWindow()
    {
        
        InitializeComponent();

        myContainers.Add(new OuterList("Start"));
        myContainers.Add(new OuterList("Middle"));
        myContainers.Add(new OuterList("End"));
        Container.ItemsSource = myContainers;

        myItems.Add(new InnerList("34545","SD5"));
        myItems.Add(new InnerList("45654", "SD5"));
        myItems.Add(new InnerList("65775", "SD5"));
        myItems.Add(new InnerList("78677", "SD5"));
        myItems.Add(new InnerList("35887", "SD5"));
        Orders.ItemsSource = myItems;  //<- The name "Orders" does not exist in the current context.

    }
}

All the Content of the OuterList will shown, but no Item from InnerList

How can i add Content from two different ItemSources? Or, what am I doing wrong?

Got an issue in Code: The name "Orders" does not exist in the current context.

Thanks for you Help!

CodePudding user response:

Because your DataContext is incorrect.

Assuming the Grid that wraps both of them its DataContext contains Outer and Inner

You can do this:

<Grid>
    <ListView x:Name="Container" ItemsSource="{Binding OuterList}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Margin="5" FontSize="24" FontFamily="Arial" Background="Red" Text="{Binding Process}"/>

                    <ListBox x:Name="Orders" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Grid}, Path=DataContext.InnerList}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <TextBlock Text="{Binding Product}"/>
                                    <TextBlock Text="{Binding Order}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

CodePudding user response:

Thank You. But i get an issue the name "Orders" does not exist in the current context. I Edit the Question and add my Code.

  • Related