• WPF如何获得ListView内各单元格控件


    ------------------
    首先不推荐这种模式
    ItemsSource 已经绑定了源. Items 集合的数量变化很随机, 每个单元格的控件取决於
      默认的DataTemplate(设置 DisplayMemberBinding 时) 或者
      CellTemplate 定义的控件, 或者
      CellTemplateSelector 指定的 Template 定义的控件.
    通过 rowIndex 和 cellIndex 来取得某单元格控件其实不是好的解决问题的办法. 你完全可以在数据层面上捕抓到改变, 然后根据改变后的值判断是否应该采用新值, 而不应在 UI 元素上下功夫.
    ------------------
    回到问题,
    如何取得在单元格中承载数据的控件? (这里所说的单元格是指, ListView 的 View 是 GridView 或其衍生类 的某一个承载数据的东西).
    要取得, 必须要了解. ListView 是一种 Items 的控件, 它的每一个 Item 都由 ListViewItem 作为容器来承载它要显示的内容.
    ListView
      ListViewItem1
      ListViewItem2
      ...
    即使在 DataTemplate 没有使用 ListViewItem , ListView 显示时也使用 ListViewItem 来承载它. 对於使用 GridView 的 ListView 来说, 每一个 ListViewItem 的控件树结构就像下面所示的:
      ListViewItem (default, non-styled)
        Border
          GridViewRowPresenter
            ContentPresenter 1
              承载数据所用的控件1 (你问题的目标控件就是这个了, 单元格1)
            ContentPresenter 2
              承载数据所用的控件2 (单元格2)
            ...
            ContentPresenter N
              承载数据所用的控件N (单元格N)
    显然, ContentPresenter 的 Content 正是我们目标所要得到的东西.
    於是有了下面的函数:
    private UIElement GetListViewCellControl(int rowIndex, int cellIndex)
    {
        // rowIndex 和 cellIndex 基於 0.
        // 首先应得到 ListViewItem, 毋庸置疑, 所有可视UI 元素都继承了UIElement:
        UIElement u = lv.ItemContainerGenerator.ContainerFromIndex(rowIndex) as UIElement;
        if(u==null) return null;
       
        // 然后在 ListViewItem 元素树中搜寻 单元格:
        while ((u=(VisualTreeHelper.GetChild(u, 0) as UIElement)) != null)
            if (u is GridViewRowPresenter) return VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(u, cellIndex), 0) as UIElement;

        return u;
    }
    这个函数就返回了单元格的控件, 是 UIElement, 对应的, 如果你 DataTemplate 是使用 TextBox 承载数据的, 它的返回值就是一个 TextBox, 如果你用 CheckBox 承载数据, 它的返回值就是一个 CheckBox, and so on, 例如:
        TextBox t = GetListViewCellControl(1, 2) as TextBox; // 第2 行,第3 格
        CheckBox t = GetListViewCellControl(3, 4) as CheckBox; // 第4 行第 5 格
      ...

    总言, 这是一种不好的方式, 这种方式使用了 WinForm 的模式来操纵数据, 它并不适合 wpf.
  • 相关阅读:
    Django(app的概念、ORM介绍及编码错误问题)
    Django(完整的登录示例、render字符串替换和redirect跳转)
    Construct Binary Tree from Preorder and Inorder Traversal
    Single Number II
    Single Number
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
    Binary Tree Zigzag Level Order Traversal
    Recover Binary Search Tree
    Add Binary
  • 原文地址:https://www.cnblogs.com/homezzm/p/1749923.html
Copyright © 2020-2023  润新知