Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I just need a little help !
Small issue while working on a app for windows.

Issue-

I want to use tag property in order to use in method(event) selection change for list view control.
My list view item template consist of StackPanel with its children - a TextBlock and a Image.

what i want is to retrieve the value in TextBlock when selection change event in occurred and use it in some other method ,so is there any possibility to do so or I could add tag to item with some value ?? if so how could I do it?
Posted
Updated 7-Sep-13 2:36am
v2
Comments
Mike Meinz 7-Sep-13 9:48am    
You can save an object in the ListViewItem Tag property. When you retrieve the object from the Tag property, cast the object as the appropriate type. If you have multiple items to save, design your own Class, instantiate the Class, populate the Class with the items then put the instantiated Class into the Tag property. When retrieving from the Tag property, cast as the user-defined Class name.
Shubhanshu Singh 8-Sep-13 3:44am    
is it possible to add a tag using xaml ,every time item is added to List View control ??????
Where Item itself consist of stackpanel with -textblock and image .
I used ItemTemplate inorder to represent each item and in code behind I set ListView ItemSource to a List<t> ,so Image and Textblock are binded to value stored in List<t> .
Mike Meinz 8-Sep-13 5:56am    
I have stored objects (classes) that I have created in the ListViewItem Tag property. I do not know enough about the classes you speak of to know if you can stored them as-is in the Tag property or if you have to serialize them to store them in the Tag property. You may have to write a little test application and try it.
Shubhanshu Singh 9-Sep-13 12:48pm    
can u just give me an example where I can store object in ListViewItem tag property ,means just the code ,then i will get an idea how to do it ...????? it will be really helpful .Thanx
Mike Meinz 9-Sep-13 14:18pm    
See Solution 1

1 solution

This is code from one of my applications. I am showing you how to (1) declare your own class to save a variety of values, (2) how to save the class into the Tag property and (3) how to retrieve the class object or one of the values stored in the class object from the Tag property.

Sample Code - Declare a class to save information in a ListViewItem Tag property
A class named EquipmentClass that will hold information that I want to store in the ListView Tag property but not display in the ListView control.
C#
class EquipmentClass
{
    internal EquipmentClass(string WO_Alert, long Code, long Type) : base()
    {
        this.m_WO_Alert = WO_Alert;
        this.m_Code = Code;
        this.m_Type = Type;
    }
    internal string WO_Alert {
        get { return m_WO_Alert; }
        set { m_WO_Alert = value; }
    }
    internal long Code {
        get { return m_Code; }
        set { m_Code = value; }
    }
    internal long Type {
        get { return m_Type; }
        set { m_Type = value; }
    }
    // Should an email be sent if there is a Work Order for this equipment
    private string m_WO_Alert; 
    // The Primary Key to access the Work Order Table to find this row
    private long m_Code; 
    // Inventory Item Type of the equipment to which this Work Order applies
    private long m_Type;
}



Sample Code - Put EquipmentClass into ListViewItem Tag property
This code demonstrates how a ListViewItem is instantiated and populated with the SubItems and the Tag property. In my program, this code is in a While loop in which data is returned from the database into an SQLDataReader object named rs.
string strAlert;
ListViewItem lstViewItem;
lstViewItem = new ListViewItem(rs("Description").ToString);
//
lstViewItem.SubItems.Add(rs("Model").ToString);
lstViewItem.SubItems.Add(rs("Serial").ToString);
lstViewItem.SubItems.Add(rs("Location").ToString);
lstViewItem.SubItems.Add(rs("Inventory_Number").ToString);
lstViewItem.SubItems.Add(rs("Desig").ToString);
lstViewItem.SubItems.Add(rs("Manufacturer").ToString);
if (rs.IsDBNull(rs.GetOrdinal("WO_Alert"))) {
         // If database value is Null, assume 'N'
	strAlert = "N";
} else {
	strAlert = Convert.ToString(rs("WO_Alert"));
}
// Put some extra data into the Tag property using the EquipmentClass Class
lstViewItem.Tag = new EquipmentClass(strAlert, Convert.ToInt64(rs("Code")), Convert.ToInt64(rs("Type_Code")));
lstInventory.Items.Add(lstViewItem);  // Add the LisViewItem to the ListView control


Sample code - Retrieving values from the TAG property
You can retrieve individual values from the EquipmentClass object within the Tag property or you could retrieve the entire object into a local variable of EquipmentClass type. Note that the Tag property is cast as the EquipmentClass class. This example retrieves only the Code property of the EquipmentClass for the ListViewItem that is at index idx in the ListView control.
myVariableName = ((EquipmentClass)lstInventory.Items(idx).Tag).Code
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900