none
Binding a ListView

    Question

  • Hello,

    normally I bind a ListView against a ObservableCollection<MyItems> for example. The class MyItems has two properties or something else. But now I want to bind against a class MyItems with a collection of strings (List<string>). One instance of MyItems stands for one line in the ListView and every item in the List<string> Collection stands for one entry in one column.

    Summarized I don't know how many columns I have in the ListView. Is there a way to bind against that collection (or could you tell me, how I should build up the collection so I can bind it?).

    If it is impossible, how can I build up the ListViewItems in code at runtime?


    Bye and thanks for help,
    Traidor
    Friday, July 11, 2008 7:01 AM

Answers

  • -> Is there a way to bind against that collection (or could you tell me, how I should build up the collection so I can bind it?).

    If I understand you correctly, you want the ObservableCollection to be the data binding target, unfortunately, this is not supported, because ObservableCollection doesn't derive from DependencyObject, and doesn't expose any dependency properties which you can bind to. Note that only dependency property can be used as the binding target.

    -> If it is impossible, how can I build up the ListViewItems in code at runtime?

    Here is a simplified example:

    ListView listView = new ListView();
    ListViewItem item = new ListViewItem();
    listView.Items.Add(item);

    hope this helps
    • Marked as answer by Marco Zhou Thursday, July 17, 2008 10:35 AM
    Monday, July 14, 2008 8:53 AM
  • You can combine ListView, ListBox & WrapPanel to accomplish your goal, here an example, sorry I just have time to build an XAML sample code for you:

    XAML code:

            <DataTemplate x:Key="CellTemplate">
                <TextBlock Text="{Binding}" />
            </DataTemplate>

            <ListView x:Name="MSDNListView" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding Path=TestStrings}" ItemTemplate="{StaticResource CellTemplate}">
                            <ListBox.Template>
                                <ControlTemplate>
                                    <WrapPanel Margin="5" IsItemsHost="True" Orientation="Horizontal"
                                           VerticalAlignment="Top" HorizontalAlignment="Stretch" />
                                </ControlTemplate>
                            </ListBox.Template>
                        </ListBox>
                    </DataTemplate>
                </ListView.ItemTemplate>

    C# Code


        public class MSDNCollection : ObservableCollection<MSDNItem>
        {
        }

        public class MSDNItem
        {
            public List<string> TestStrings { get; set; }

            public string Name { get; set; }
           
            public MSDNItem()
            {
                TestStrings = new List<string>();
            }

            public MSDNItem(string name) : this()
            {
                Name = name;
            }
        }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                MSDNCollection coll1 = new MSDNCollection();
                MSDNItem item = new MSDNItem("Item1");
                for (int i = 0; i < 10; ++i)
                    item.TestStrings.Add(i.ToString());
                coll1.Add(item);

                item = new MSDNItem("Item2");
                for (int i = 0; i < 8; ++i)
                    item.TestStrings.Add(i.ToString());
                coll1.Add(item);

                MSDNListView.DataContext = coll1;
            }

    Hope this can resolve your issue.

    • Marked as answer by Marco Zhou Thursday, July 17, 2008 10:35 AM
    Monday, July 14, 2008 10:43 AM

All replies

  • -> Is there a way to bind against that collection (or could you tell me, how I should build up the collection so I can bind it?).

    If I understand you correctly, you want the ObservableCollection to be the data binding target, unfortunately, this is not supported, because ObservableCollection doesn't derive from DependencyObject, and doesn't expose any dependency properties which you can bind to. Note that only dependency property can be used as the binding target.

    -> If it is impossible, how can I build up the ListViewItems in code at runtime?

    Here is a simplified example:

    ListView listView = new ListView();
    ListViewItem item = new ListViewItem();
    listView.Items.Add(item);

    hope this helps
    • Marked as answer by Marco Zhou Thursday, July 17, 2008 10:35 AM
    Monday, July 14, 2008 8:53 AM
  • You can combine ListView, ListBox & WrapPanel to accomplish your goal, here an example, sorry I just have time to build an XAML sample code for you:

    XAML code:

            <DataTemplate x:Key="CellTemplate">
                <TextBlock Text="{Binding}" />
            </DataTemplate>

            <ListView x:Name="MSDNListView" ItemsSource="{Binding}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding Path=TestStrings}" ItemTemplate="{StaticResource CellTemplate}">
                            <ListBox.Template>
                                <ControlTemplate>
                                    <WrapPanel Margin="5" IsItemsHost="True" Orientation="Horizontal"
                                           VerticalAlignment="Top" HorizontalAlignment="Stretch" />
                                </ControlTemplate>
                            </ListBox.Template>
                        </ListBox>
                    </DataTemplate>
                </ListView.ItemTemplate>

    C# Code


        public class MSDNCollection : ObservableCollection<MSDNItem>
        {
        }

        public class MSDNItem
        {
            public List<string> TestStrings { get; set; }

            public string Name { get; set; }
           
            public MSDNItem()
            {
                TestStrings = new List<string>();
            }

            public MSDNItem(string name) : this()
            {
                Name = name;
            }
        }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                MSDNCollection coll1 = new MSDNCollection();
                MSDNItem item = new MSDNItem("Item1");
                for (int i = 0; i < 10; ++i)
                    item.TestStrings.Add(i.ToString());
                coll1.Add(item);

                item = new MSDNItem("Item2");
                for (int i = 0; i < 8; ++i)
                    item.TestStrings.Add(i.ToString());
                coll1.Add(item);

                MSDNListView.DataContext = coll1;
            }

    Hope this can resolve your issue.

    • Marked as answer by Marco Zhou Thursday, July 17, 2008 10:35 AM
    Monday, July 14, 2008 10:43 AM