locked
Binding problem: SelectedIndex always 0 RRS feed

  • Question

  • I have a listview binded to an observable collection. My problem is that the selectedindex is always 0.
    After I add several items, and set ListView1.SelectedIndex = 1, it will still be 0. Mouse clicks don't change the selectedindex either.

    Can anyone give me some suggestion about what I should look for? To me it seems that code and WPF are fine (I've another listview, right now outcommented, that has almost the same code and works fine).

    Thanks in advance for any help.


    declaration:

    public struct teststruct
    {
    public string Name { get; set; }
    public string Help { get; set; }
    }

    public static ObservableCollection<teststruct> _Test = new ObservableCollection<teststruct>();

    public ObservableCollection<teststructTest{ get { return _Test; } }


    code:
    _Test.Add(new teststruct());
    _Test.Add(
    new teststruct());
    _Test.Add(
    new teststruct());

    WPF:

    <Window x:Class="MyProg.UI.wFilters"
    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition Height="25" />
    <RowDefinition Height="25" />
    <RowDefinition Height="25" />
    <RowDefinition />
    <RowDefinition Height="25" />
    <RowDefinition />
    <RowDefinition Height="25" />
    </Grid.RowDefinitions>

    <
    ListView Grid.Row="4" Grid.ColumnSpan="2" Name="lvFilters" ItemsSource="{Binding Test}" >
    <ListView.View>
    <GridView>
    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="150" />
    <GridViewColumn Header="Description" DisplayMemberBinding="{Binding Help}" />
    </GridView>
    </ListView.View>
    </ListView>

    </Grid>
    </
    Window>

    Monday, December 1, 2008 2:43 PM

Answers

  • Hi Patrick,


    What is happening here is that you add 3 the same items to the listview.

    If you would change the struct to class, they would become reference types and they would not be the same anymore (because the reference is different).

    But now you use value types and both Name and Help are null for the 3 items so they are equal. (the Equal method will use reflection to compare the items).

     

    If you change this your example will work just fine:

    _Test.Add(new teststruct() { Name = "1", Help = "4" });  
    _Test.Add(new teststruct() { Name = "2", Help = "5" });  
    _Test.Add(new teststruct() { Name = "3", Help = "6" }); 

     

     

    I don’t really know how WPF handles this but what is probably happening is:

    You click on the item.

    The SelectedItem is set the the one you clicked (the SelectedIndex will return the index of the first item it finds in the collection)
    The listview sets the IsSelected property of the first ListviewItem it can find to true.

     

    Or something similar…


    Kevin

    • Proposed as answer by KCSharp Tuesday, December 2, 2008 10:05 AM
    • Marked as answer by _patrick_ Tuesday, December 2, 2008 11:14 AM
    Tuesday, December 2, 2008 10:04 AM

All replies

  • Hi Patrick,


    What is happening here is that you add 3 the same items to the listview.

    If you would change the struct to class, they would become reference types and they would not be the same anymore (because the reference is different).

    But now you use value types and both Name and Help are null for the 3 items so they are equal. (the Equal method will use reflection to compare the items).

     

    If you change this your example will work just fine:

    _Test.Add(new teststruct() { Name = "1", Help = "4" });  
    _Test.Add(new teststruct() { Name = "2", Help = "5" });  
    _Test.Add(new teststruct() { Name = "3", Help = "6" }); 

     

     

    I don’t really know how WPF handles this but what is probably happening is:

    You click on the item.

    The SelectedItem is set the the one you clicked (the SelectedIndex will return the index of the first item it finds in the collection)
    The listview sets the IsSelected property of the first ListviewItem it can find to true.

     

    Or something similar…


    Kevin

    • Proposed as answer by KCSharp Tuesday, December 2, 2008 10:05 AM
    • Marked as answer by _patrick_ Tuesday, December 2, 2008 11:14 AM
    Tuesday, December 2, 2008 10:04 AM
  • Thanks a lot Kevin!! I didn't think about that. Personally I think WPF is doing things the wrong way, but now I know what's going on, it doesn't really matter.

    Best regards,
    Patrick
    Tuesday, December 2, 2008 11:21 AM