0

I'm building a User Control with ListView of n columns, so I have a List of Lists:

C#:

public List<List<string>> ListItems { get; set; }

var itemList = new List<string>();
itemList.Add("1");
itemList.Add("John Doe");

ListItems.Add(itemList);

itemList = new List<string>();
itemList.Add("2");
itemList.Add("Jane Doe");

ListItems.Add(itemList);

itemList = new List<string>();
itemList.Add("3");
itemList.Add("Sammy Doe");

ListItems.Add(itemList);

XAML:

<ListView Name="lvwLista" Grid.Row="1" SelectionMode="Single" ScrollViewer.IsDeferredScrollingEnabled="False" ItemsSource="{Binding ListItems, ElementName=ucListaCompletarControl}">
 <ListView.ItemTemplate>
  <DataTemplate>
    <Label Content="{Binding Path=.}"/>
  </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

But I don't Know how to binding the List<List<string>> to ListView.

Thanks.

3 Answers 3

1

You are accessing List<string> in the ItemTemplate and so you can bind its properties as always.

<ListView Name="lvwLista"
          ItemsSource="{Binding ListItems, ElementName=ucListaCompletarControl}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Path=Item[0]}"/>
                <Label Content="{Binding Path=Item[1]}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Or

<ListView Name="lvwLista"
          ItemsSource="{Binding ListItems, ElementName=ucListaCompletarControl}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ListView ItemsSource="{Binding}">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this works, but I populating the lists with a loop and I don't know the columns number. How I can show in DataTemplate all columns?
If so, add ListView or other ItemsControl in DataTemplate.
Thanks, works but I need add a click event in the rows and this complicate this function, sorry hehe.
0

Following link titled; "Binding a list of lists to a GridView which contains GridLists in WPF" maybe helpful for you.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/9e04b734-ba83-47d5-a50e-d0544d28e69f/binding-a-list-of-lists-to-a-gridview-which-contains-gridlists-in-wpf?forum=wpf

1 Comment

Thanks but I don't know the columns number in Listview, can be 2, 5, etc.
0

Thanks everybody, my solution was a string with items in only row and add it to List:

C#:

public static List<string> ListItems { get; set; }

for(...)
{
 for(...)
 {
  var itemList = string.Empty;
  itemList += item.ToString() + "\t";
 }
 ListItems.Add(itemList);
}

XAML:

<ListView Name="lvwLista" Grid.Row="1" SelectionMode="Single" 
        ScrollViewer.IsDeferredScrollingEnabled="False"
        ItemsSource="{Binding ListItems, ElementName=ucListaCompletarControl}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding}"/>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.