欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

WPF 绑定LISTVIEW 子控件的事件

程序员文章站 2022-03-07 17:04:01
...

1.我们可以用数据模版实现添加按钮

    <ListView x:Name="Liv">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Index" DisplayMemberBinding="{Binding Path=Index}" />
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}" />
                <GridViewColumn Header="Info">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Width="30" Content="..." Click="BtnInfo_Click" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

点击按钮时 通过按钮的 DataContent 获取绑定的对象

    private void BtnInfo_Click(object sender, RoutedEventArgs e)
    {
        var btn = sender as Button;
        var player = btn.DataContext as Player;
    }