I have a button in my GridView. Once i click the button I need to select the current row.

I could see a sample in WPF Samples using a check box but couldnt customize it for a button. The code for check box is as below

<DataTemplate x:Key="FirstCell">

<StackPanel Orientation="Horizontal">

<CheckBox IsChecked="{Binding Path=IsSelected,

RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}}"/>



which is refered in



<GridViewColumn CellTemplate="{StaticResource FirstCell}"




I need to replace the checkbox with a button and on Click event I need to select the current row

Can anyone tell me how is it possible

Re: Windows Presentation Foundation (WPF) Select Current Row on Button Click in a Grid View

Yi-Lun Luo - MSFT

Hello, when you click any place inside a GridView’s row, the row will automatically be selected. Why do you bother to use a Button to select it

Re: Windows Presentation Foundation (WPF) Select Current Row on Button Click in a Grid View

Joel Conner

I have a similar problem, but using a TextBox rather than a button. The problem is that clicking the TextBox gives it the focus (as intended... to allow editing the contents). I would expect that WPF would also select the row at that point, but it does not. So you get an odd looking condition where one row is selected, but you are editing data in a different row.

Using a TextBlock, which does not get the focus, selects the row but is, of course, not editable.

Re: Windows Presentation Foundation (WPF) Select Current Row on Button Click in a Grid View


Sujath and Joel,

You are both correct. If your selector (ListBox, ListView, etc) contains an element that handles the mouse event, then the automatic selection will not occur. In that case, you must find the corresponding item container and set its IsSelected property. Alternatively, you can set the SelectedIndex property on the selector.

There are a number of ways you can find the item container. I prefer to walk the tree of visual ancestors because it provides a more reliable solution when the bound collection contains value types.

Assume you have a ListView named myListView. For your scenario, Sujath, you could add the following code to your button's Click handler:

Code Snippet

public void ButtonClick(object sender, RoutedEventArgs e)


ListViewItem lvi = GetAncestorByType(

e.OriginalSource as DependencyObject,

typeof(ListViewItem)) as ListViewItem;

if (lvi != null)


myListView.SelectedIndex =




public static DependencyObject GetAncestorByType(DependencyObject element, Type type)


if (element == null) return null;

if (element.GetType() == type) return element;

return GetAncestorByType(VisualTreeHelper.GetParent(element), type);


Joel, you would do the exact same thing in the GotFocus handler of your TextBox.

Notice that in the sample above I set the SelectedIndex property on the ListView. You could also just set lvi.IsSelected=true, but you may not get the desired behavior if the SelectionMode for your ListView is not Single.

Another approach is to use the ItemContainerGenerator.IndexFromItem() method on the DataContext of the templated item (the Button or TextBox). Note that if the source collection contains value types and there are duplicate values in the collection, then the IndexFromItem() method is unreliable. It will always return the first instance of the specified item.