访问WPF ListView的自定义元素

时间:2016-04-08 08:02:57

标签: c# wpf listview

我有这个WPF ListView:

<ListView x:Name="OracleListView">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Selected">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Checked="Item_Checked" Unchecked="Item_Unchecked"/>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

我这样填写:

OracleListView.Items.Add(new ListBoxItem{Content="Table A"});
OracleListView.Items.Add(new ListBoxItem{Content="Table B"});
OracleListView.Items.Add(new ListBoxItem{Content="Table C"});

所以我有listview有3个复选框和3个字符串。问题是:

如何访问与所选项目对应的复选框列?

1 个答案:

答案 0 :(得分:1)

您可以轻松访问已定义的cheked事件处理程序中的相应复选框:

private void Item_Checked(object sender, RoutedEventArgs e)
{
    var checkBox = e.Source as CheckBox;    
}