Joan Venge
Joan Venge

Reputation: 330862

How to use the Click event in a GridViewColumn on a WPF TreeView/ListView?

Currently I have some GridViewColumns such as this:

<GridViewColumn Width="20">
    <GridViewColumnHeader Content="X" />
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Width="18"
                Height="18"
                Source="{Binding VisibleIcon}"
                Opacity="{Binding VisibleOpacity}"/>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

where I want to do something on the item that's clicked on this column. But I don't know how to add the Click event because neither GridViewColumn nor Image has a Click event.

I was hoping for something like this:

void ClickEvent ...
{
    SelectLayer (boundDataItem);
    // boundDataItem is of type `Layer`.
}

EDIT: I found a MouseLeftButtonDown event but that doesn't give me the clicked bound data item, can only get the image or text but they aren't unique for an image to lookup.

EDIT2: Another thing is even with MouseLeftButtonDown it only works if I click on the TextBlock itself, not outside it even within the same GridViewColumn.

Upvotes: 1

Views: 2250

Answers (1)

default.kramer
default.kramer

Reputation: 6103

You can add the handler on your root element (StackPanel.MouseLeftButtonDown), then in your handler you can get to the bound item using var layer = (sender as FrameworkElement).DataContext as Layer.

Upvotes: 1

Related Questions