0

I have a ListView that uses data templates to display specific controls within the cells:

            <ListView Grid.Row="1" ItemsSource="{Binding Ratings}" SelectedItem="{Binding SelectedRating}"  Margin="5" MinHeight="50" SelectionMode="Single" ScrollViewer.CanContentScroll="True"
                  local:GridViewSort.Enable="True">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Date" local:GridViewSort.PropertyName="RatingDate">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <DatePicker SelectedDate="{Binding RatingDate}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Rating ID" local:GridViewSort.PropertyName="RatingID">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding RatingID}" Width="35" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Name" local:GridViewSort.PropertyName="RatingName">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBox Text="{Binding RatingName}" Width="35" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Age" local:GridViewSort.PropertyName="RatingAge">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBox Text="{Binding RatingAge}" Width="35" />
                                    <TextBlock Text=" yrs"/>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>

I also have a delete button to remove the selected row.

My user has a habit of clicking into to the edit and date controls (which doesn't change the selected row) and then hitting delete to remove the row he has clicked on. Unfortunately, this deletes the wrong row.

How can I change the row selection whenever the user clicks into the controls?

1 Answer 1

2

You can use PreviewMouseUp event on a ListViewItem:

<ListView Grid.Row="1" ItemsSource="{Binding Ratings}" SelectedItem="{Binding SelectedRating}"  Margin="5" MinHeight="50" SelectionMode="Single" ScrollViewer.CanContentScroll="True"
                  local:GridViewSort.Enable="True">
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <EventSetter Event="PreviewMouseUp" Handler="ListViewItem_PreviewMouseUp" />
        </Style>
    </ListView.Resources>
    <ListView.View>
        <GridView>
        ...

Code behind:

private void ListViewItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
    var listViewItem = sender as ListViewItem;
    if (listViewItem != null)
    {
        listViewItem.IsSelected = true;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.