The following xaml code produces a ListView with three columns. The ListView ItemsSource is an observablecollection. The first column shows the name of the object in a particular row. The second and third columns show buttons associated to the object in a particular row.
<Grid Width="497" Height="260">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="309*" />
<ColumnDefinition Width="188*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="478*" />
<RowDefinition Height="4*" />
</Grid.RowDefinitions>
<GroupBox Header="ObservableCollection Object List" HorizontalAlignment="Left" Width="497" BorderBrush="Black" BorderThickness="2" Grid.ColumnSpan="2">
<ListView ItemsSource="{Binding Path=CollectionList}" Name="QueueListView">
<ListView.Resources>
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewGotKeyboardFocus" Handler="SelectCurrentItem"/>
</Style>
</ListView.Resources>
<ListView.SelectedItem>
<Binding Path="SelectedObjectFromList" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
</Binding>
</ListView.SelectedItem>
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="Object Name" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="160" Header="Property Information">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Get Property Info" Command="{Binding Path=GetObjProperties}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="180" Header="Transfer">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Transfer Object" Command="{Binding Path=TransferObjHere}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</GroupBox>
</Grid>
The SelectCurrentItem event handler binds the ListView.SelectedItem to the SelectedObjectFromList property in my view model when the user clicks the “Transfer Object” or “Get Property Info”button. I an use this property to expose the selected object to my view model.
Here is my SelectCurrentItem handler c# code in code behind:
protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
{
ListViewItem viewItem = (ListViewItem)sender;
viewItem.IsSelected = true;
}
This works great! When the user has clicked a button, the SelectedObjectFromList property is updated from the ListView observablecollection object for that row. (No need to manually click the ListView row to set the property before clicking the button.)
One problem: As I click buttons in the list, the recently selected rows still appear to be selected (they are highlighted in the GUI).
I tried to solve the problem by setting the isFocused property of the ListView:
protected void SelectCurrentItem(Object sender, KeyboardFocusChangedEventArgs e)
{
ListViewItem Item = (ListViewItem)sender;
Item.IsSelected = true;
Item.IsFocused = true;
}
Of course that gives me a StackOverflow exeption :). Does anyone have example code to update the ListView.Selection in the GUI in this case? Thanks in advance.
ListViews allow you to have multiple items selected at once. When you click on a 2nd item, you’re not resetting the
IsSelectedproperty of the old selected item to False, so it is staying selectedYou can try either setting the ListView’s
SelectionModeto Singleor try getting the old SelectedItem and unselecting it when you’re selecting the new one