Click here to Skip to main content
14,668,708 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello,
I have this listView in xaml:

<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:
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?

Rate this:
Please Sign up or sign in to vote.

Solution 2

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

i.e.

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.

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.
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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?
   
v2

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100