Selecting WPF ListView row in code
An app I am considering migrating to WPF uses a DevExpress grid to show a list of events, as I animate my composition I highlight the current event in the grid. Things I have learned about WPF:
01: You can create a data grid type view like so:
<ListView Name="ListView">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn DisplayMemberBinding="{Binding Path=Title}" Header="Title"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="FirstName"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=LastName}" Header="LastName"/>
</GridView>
</ListView.View>
</ListView>
02: Don’t put a ListView in a <StackPanel>! It renders all of the data at once. When you have 6K rows of data for example it takes about 1 second to select the next row. Instead you should put it in a grid
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0"...../>
<ListView Grid.Row="1"...../>
</Grid>
03: When selecting a row in code the row does not become visible. To automatically make the row visible you need to use ScrollIntoView()
ListView.SelectedIndex++;
ListView.ScrollIntoView(ListView.SelectedItem);
01: You can create a data grid type view like so:
<ListView Name="ListView">
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn DisplayMemberBinding="{Binding Path=Title}" Header="Title"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=FirstName}" Header="FirstName"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=LastName}" Header="LastName"/>
</GridView>
</ListView.View>
</ListView>
02: Don’t put a ListView in a <StackPanel>! It renders all of the data at once. When you have 6K rows of data for example it takes about 1 second to select the next row. Instead you should put it in a grid
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0"...../>
<ListView Grid.Row="1"...../>
</Grid>
03: When selecting a row in code the row does not become visible. To automatically make the row visible you need to use ScrollIntoView()
ListView.SelectedIndex++;
ListView.ScrollIntoView(ListView.SelectedItem);
Comments
Is it possible to scroll the item into view such that it is shown in the middle of all VISIBLE records, instead of on the top.
RL from Canada