0

我有这个带有 TextBock 和 CheckBox 的 ListView:

<ListView x:Name="ActiveTasksListView" ItemsSource="{Binding Path=ActiveTasks}" SelectedItem="{Binding Id}">

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical" Margin="37,0,17,0" />
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

<ListView.ItemTemplate>
    <DataTemplate>
        <Border Background="White">
            <TextBlock Text="{Binding Name}" />
            <CheckBox Click="CheckBox_Click_1" />
        </Border>
    </DataTemplate>
</ListView.ItemTemplate>

</ListView>

如果我单击 CheckBox,则需要在 MessageDialog() 中显示单击项目的 Id(来自数据源)

4

2 回答 2

0

使用 VisualTreeHelper 并单击 CheckBox 元素,您可以找到 ListViewItem 然后获取其 Id 属性

void CheckBox_Click_1(object sender, RoutedEventArgs e)
{
    var item = sender as DependencyObject;

    while (!(item is ListViewItem))
    {
        item = VisualTreeHelper.GetParent(item);
    }

    var id = ((ObjectClass) item).Id;
}
于 2013-03-16T00:14:31.427 回答
0
void CheckBox_Click_1(object sender, RoutedEventArgs e)
{
    var cb = sender as CheckBox;
    dynamic itemVM = cb.DataContext;
    var id = itemVM.Id;
}
于 2013-03-16T03:47:30.517 回答