Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,
I have this listView in xaml:

XML
<ListView Height="100" HorizontalAlignment="Left" Margin="32,196,0,0" Name="listView1" VerticalAlignment="Top" Width="438">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="ID" DisplayMemberBinding="{Binding ID}"/>
            <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Description}"/>
        </GridView>
    </ListView.View>
</ListView>



and i am adding items from code:
C#
listView1.Items.Add(new {ID="item1", Description="item2" } );
listView1.Items.Add(new {ID="item3", Description="item4" } );
listView1.Items.Add(new {ID="item5", Description="item6" } );


The problem is that always the first item is selected, and i want to select other items, clicking on other items wont work, what should i do?
Posted
Updated 18-Oct-11 4:02am
v2
Comments
Wayne Gaylard 18-Oct-11 10:09am    
Your code works for me. What is it exactly you are trying to achieve?

You can use custom objects rather than annonomous. That should solve it.

i.e.

C#
public class CustomListItem
{
    public string ID {get; set;}
    public string Description {get; set;}
}

...

listView1.Items.Add(new CustomListItem(){ID = "item1", Description="item2"};


Also if you want a specific item to be selected you can then hold on to it.

C#
CustomListItem _default = null;
...

_default = new CustomListItem(){ID = "item1", Description="item2"};
listView1.Items.Add(_default);

...
listView1.SelectedItem = _default;


FYI - the reason being is that the annonomous objects are on the stack (I believe... I dont use them really but the behavior leads one to this assumption). By making your own object it is reference based so even if the props are the exact same, they are different. Until you override the Equals and HashCode to say otherwise.
 
Share this answer
 
v2
i figured it out, it's like OMG.

i cant have the same items in the list:

listView1.Items.Add(new {ID="item1", Description="item2" } );
listView1.Items.Add(new {ID="item1", Description="item2" } );
listView1.Items.Add(new {ID="item1", Description="item2" } );
listView1.Items.Add(new {ID="item1", Description="item2" } );
listView1.Items.Add(new {ID="item1", Description="item2" } );


because it will always select the first one.

any solution if i do have the same items?
 
Share this answer
 
v2

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