首页 > 解决方案 > 如何在 ListView 中获取复选框的坐标

问题描述

在我的项目中,我有一个 ListView,里面有很多复选框。为了一次选中和取消选中它们的选择,我实现了在画布 (dragSelectionCanvas) 内绘制矩形 (dragSelectionBorder) 的功能,与此处所做的非常相似:https ://www.codeproject.com/Articles /148503/WPF 中的简单拖动选择

现在我想比较一下,ListView 中的哪些复选框(部分)在 dragSelectionBorder 内。对于dragSelectionBorder,可以相对于dragSelectionCanvas访问Coords,但是如何获取Checkboxes的坐标呢?我是否需要将我所有的 xaml 设计设置为这样的 Canvas,还是不需要外部 Canvas?

<Window>
    <Canvas x:Name="IsThisCanvasNeeded?">
        <Grid 
            x:Name="TheGridContainingEverything">
            <ListView x:Name="ManyCheckBoxes"/>
            <Canvas
            x:Name="dragSelectionCanvas">
                <Border 
                     x:Name="dragSelectionBorder"/>
            </Canvas>
        </Grid>
    </Canvas>
</Window>

要检查每个 CheckBox,如果它在 dragSelectionBorder 内,我想循环遍历 ListView,就像这样。

foreach (var item in MyListView.Items)
            {
                var lvi = TipRackListViewTopLeft.ItemContainerGenerator.ContainerFromItem(item) as FrameworkElement;
                // Get the Checkbox that is in the lvi
                // get the coords of the Checkbox
                // generate a Rectangle out of the checkbox coords
                // if(SelectionRectangle.Contains(CheckboxRectangle))
                         CheckBox.IsChecked = !CheckBox.IsChecked;
            }

我希望你能帮我解决这个问题,因为我现在挣扎了好几个小时。

谢谢,

马丁

标签: listviewcanvaspositioncontrolscoordinates

解决方案


推荐阅读