tags:

views:

8840

answers:

6

How it can be done in C#?

A: 

Hi, If I understood your question correctly then the following code snippet taken from http://msdn.microsoft.com/en-us/library/system.windows.forms.listviewitem.aspx should help.

    private void CreateMyListView()
    {
        // Create a new ListView control.
        ListView listView1 = new ListView();
        listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

        // Set the view to show details.
        listView1.View = View.Details;
        // Select the item and subitems when selection is made.
        listView1.FullRowSelect = true;
        // Display grid lines.
        listView1.GridLines = true;
        // Sort the items in the list in ascending order.
        listView1.Sorting = SortOrder.Ascending;

        // Create three items and three sets of subitems for each item.
        ListViewItem item1 = new ListViewItem("item1",0);
        // Place a check mark next to the item.
        item1.Checked = true;
        item1.SubItems.Add("1");
        item1.SubItems.Add("2");
        item1.SubItems.Add("3");
        ListViewItem item2 = new ListViewItem("item2",1);
        item2.SubItems.Add("4");
        item2.SubItems.Add("5");
        item2.SubItems.Add("6");
        ListViewItem item3 = new ListViewItem("item3",0);
        // Place a check mark next to the item.
        item3.Checked = true;
        item3.SubItems.Add("7");
        item3.SubItems.Add("8");
        item3.SubItems.Add("9");

        // Create columns for the items and subitems.
        listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
        listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
        listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
        listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

        //Add the items to the ListView.
                listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

        // Create two ImageList objects.
        ImageList imageListSmall = new ImageList();
        ImageList imageListLarge = new ImageList();

        // Initialize the ImageList objects with bitmaps.
        imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
        imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
        imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
        imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

        //Assign the ImageList objects to the ListView.
        listView1.LargeImageList = imageListLarge;
        listView1.SmallImageList = imageListSmall;

        // Add the ListView to the control collection.
        this.Controls.Add(listView1);
    }

HTH Best Regards @nand

Anand
Good, but it is not WPF version of ListView...
Vladimir Aks
A: 

If you're using the ListView as it is intended it will be bound to an underlying list of some kind of Object, and that class should implement INotifyChanged. In WPF you don't directly add/remove items from the ListView, you deal with the bound list structure, and it notifies the UI of the change, which then cleverly redraws itself with the new items.

MrTelly
A: 

This is how you would add a ListViewItem created in code to your ListView:

myListView.Items.Add(new ListViewItem { Content = "This is an item added programmatically." });

However, I agree with MrTelly that this shouldn't be necessary, you should be setting ListView.ItemsSource to some collection rather than manipulating ListView.Items directly.

If you give us more details about what you want to accomplish maybe we can help you do it the WPF way, which is not always the easy way at first, but it's much easier in the long run.

Robert Macnee
A: 

You can add columns dynamically to a ListView by using Attached Properties. Check out this article on the CodeProject it explains exactly that...

WPF DynamicListView - Binding to a DataMatrix

Tawani
+3  A: 

I asked the exact same question HERE (and answered myself - I had a problem in my code). Hope it helps.

JohnIdol
+1  A: 

Adding it directly to the ListView in your App isn't necessarily the "WPF-way". Consider this:

public class BindableListViewModel 
{

     public IList<TypeOfObjectToDisplay> AllObjectsToDisplay;
     public ICommand AddNewItemToList;

     public BindableListViewModel()
     { 
       AllObjectsToDisplay = new ObservableList<TypeOfObjectToDisplay>();
       AddNewItemToList = new RelayCommand(AddNewItem(), CanAddNewItem());
     }

     public bool CanAddNewItem()
     {
       //logic that determines IF you are allowed to add
       //For now, i'll just say that we can alway add.
        return true;
     }

     public void AddNewItem()
     {
       AllObjectsToDisplay.Add(new TypeOfObjectToDisplay());
     }

}

Then, in XAML all that we need to do is bind the ItemsSource of our ListView to our AllObjectsToDisplay list. This allows us to separate the dependency of adding objects directly to our ListView; we can us WPF's strength of Data Binding to remove the direct dependency on HOW we add businesss objects to our UI container we display them in -- a very useful practice!

Jimmy Lyke