开发者

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 h开发者_JS百科as 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜