0

How to: Fill a ListView by using C# instead of using XAML

I would like to fill a ListView by using C# (WPF), not by using XAML. The reason for this is, that we do not know the number of elements before runtime.

This is my working XAML code:

<ListView Name="listView_BusinessContacts" SelectionMode="Single">
                <ListViewItem Selected="ListViewItem_Selected">
                    <DockPanel DockPanel.Dock="Top" Name="dockPanel_1">
                        <Image DockPanel.Dock="Left" Source="/X;component/Images/folder.png" Stretch="None" />
                        <Label Content="Test 123" DockPanel.Dock="Right" Name="label_1" />
                    </DockPanel>
                </ListViewItem>
            </ListView>

My idea is first to create the ListViewItem. After that, I could create the DockPanel. But now, I do not know, how to add two elements to a DockPanel (here: Image and Label). After that, I would add the DockPanel to the ListViewItem and than I would add that ListViewItem to the ListView.

I hope, that you understand what I want to do.

Solution by SynerCoder:

public void SetListViewItems()
    {
        foreach (var item in File.ReadAllLines(@"C:\Companies\Companies.txt", Encoding.UTF8))
        {
            Image image = new Image();
            image.Source = new BitmapImage(new Uri(@"Images\folder.png", UriKind.Relative));
            image.Stretch = Stretch.None;

            Label label = new Label();
            label.Content = "Test 123";

            DockPanel.SetDock(image, Dock.Left);
            DockPanel.SetDock(label, Dock.Right);

            DockPanel dockPanel = new DockPanel();
            dockPanel.Children.Add(image);
            dockPanel.Children.Add(label);

            ListViewItem listViewItem = new ListViewItem();
            listViewItem.Content = dockPanel;

            listView_BusinessContacts.Items.Add(listViewItem);
        }
    }
sjantke
  • 605
  • 4
  • 9
  • 35
  • The right approach would be to assign a DataTemplate to the ListView's `ItemTemplate` property and bind the `ItemsSource` property to a collection of data items. See the [Data Templating Overview](http://msdn.microsoft.com/en-us/library/ms742521.aspx) article on MSDN. The standard WPF books also explain this in detail. – Clemens May 20 '14 at 11:40
  • If you bind the `ItemsSource` of the `ListView` to a collection in your viewmodel you don't need to know the number of elements that will be in the `ListView`. You just need to define the template for how an item in that list will be visually represented, and can then further bind properties of the item to properties of UI elements. – Mashton May 20 '14 at 11:42
  • Thanks, Clemens and Mashton. – sjantke May 20 '14 at 12:03
  • @Exception You really shouldn't do it that way. WPF provides extremly powerful Data Templating, which would *drastically* simplify and thus improve your application. Read the article I've linked in my previous comment. – Clemens May 20 '14 at 14:29

1 Answers1

0

You can use the following code to create your listview, use the static SetDock method of the DockPanel to set the docking positions.

var listView = new ListView();
foreach (var item in something)
{
    var image = new Image();
    image.Source = ...;
    image.Stretch = Stretch.None;

    var label = new Label();
    label.Content = "Test 123";

    DockPanel.SetDock(image, Dock.Left);
    DockPanel.SetDock(label, Dock.Right);

    var dockPanel = new DockPanel();
    dockPanel.Children.Add(image);
    dockPanel.Children.Add(label);
    var item = new ListViewItem();
    item.Content = dockPanel;

    listView.Items.Add(item);
}
SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • Thanks, it works! Perfect code. Just tell me please, why do you use `var` instead of `Image`, `Label`, ... ? --> Please tell me, how can I use the read text from my file as the `Content` of the `Label`? – sjantke May 20 '14 at 11:58
  • in your foreach loop you have `foreach(item in File.ReadAllLines(...))` item is a string of the line. so you can use: `label.Content = item;` – SynerCoder May 20 '14 at 12:27
  • and I use var because it is easier to read, makes for more clean code, `ListViewItem item = new ListViewitem();` in this line it states twice that it is a `ListViewItem`, but if you state it once, it is still clear, but your code is less cluttered. With all objects I use var except for basics (int, double, string, bool, char etc) because with the basics you never use for example `int something = new int(5);` so keep stating it once I use `int something = 5;` – SynerCoder May 20 '14 at 12:30
  • -1 your answer misguided the OP, who's now really struggling to get things done due to not using the proper approach (XAML + DataBinding) as opposed to this horrible hack you posted as an answer. – Federico Berasategui May 23 '14 at 14:38
  • @HighCore I answered the question of the OP as asked, the OP all ready got suggestions in comment about databinding, if the OP chooses to dive into databinding and comes across problems he can always ask here. This question address a problem and I proposed a solution. I also answered a question once with a descent solution, http://stackoverflow.com/a/12820444/637425 but that answer was not accepted because the answer that directly addressed the problem was accepted. So I get your point, but this is what the OP asked for. – SynerCoder May 23 '14 at 14:50