有一个Listbox,里面的Item是通过数据模板生成的,如下所示:

<Border Margin="15" BorderBrush="Aqua" BorderThickness="2" Padding="8" CornerRadius="5">
      <StackPanel>
        <ListBox Name="myListBox" ItemTemplate="{StaticResource myDataTemplate}"
                 IsSynchronizedWithCurrentItem="True">
          <ListBox.ItemsSource>
            <Binding Source="{StaticResource InventoryData}" XPath="Books/Book"/>
          </ListBox.ItemsSource>
        </ListBox>
        <Button Margin="10"
                Click="DataTemplateFindElement">Get text of textBlock in DataTemplate</Button>
      </StackPanel>
    </Border>

Listbox使用的数据模板如下:

<DataTemplate x:Key="myDataTemplate">
      <TextBlock Name="textBlock" FontSize="14">
        <TextBlock.Text>
          <Binding XPath="Title"/>
        </TextBlock.Text>
      </TextBlock>
    </DataTemplate>

使用下面的代码就可以获取数据模板里面TextBlock中的内容了。

ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;    
  TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);
MessageBox.Show("The text of the named TextBlock in the DataTemplate of the selected list item: " + myTextBlock.Text

上面代码调用的函数如下:

private childItem FindVisualChild<childItem>(DependencyObject obj) where childItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is childItem)
                return (childItem)child;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
    }

WPF中如何使用代码操作数据模板生成的控件的更多相关文章

  1. WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法

    原文:WPF中ContextMenu(右键菜单)使用Command在部分控件上默认为灰色的处理方法 问题描述 今天发现如果我想在一个TextBlock弄一个右键菜单,并且使用Command绑定,结果发 ...

  2. WPF中获取Hwnd与窗体,Uid获取控件

    void MapControl_Loaded(object sender, RoutedEventArgs e) { this.OnApplyTemplate(); CurrentMapChanged ...

  3. eclipse中添加Java代码注释模板

    eclipse中添加Java代码注释模板 1.Window->Preference->Java->Code Style->Code Template,进入注释编辑界面 2.文件 ...

  4. arrays.xml中使用integer-array引用drawable图片资源,代码中如何将这些图片资源赋值到ImageView控件中

    当我们在arrays.xml文件中声明一些图片资源数组的时候: <?xml version="1.0" encoding="utf-8"?> < ...

  5. CStatic中保持图形比例不变,尽量填充控件空间的代码

    CStatic中保持图形比例不变,尽量填充控件空间的代码 先获取控件的高.宽,然后获取图像的高.宽,测试需要调整高还是调整宽 void CImagePreviewStatic::DrawItem(LP ...

  6. 《Programming WPF》翻译 第3章 3.内嵌控件

    原文:<Programming WPF>翻译 第3章 3.内嵌控件 WPF提供了一系列内嵌控件.其中大多数符合标准的你已经熟悉的Windows控件类型.注意到没有一个是包装在旧的Win32 ...

  7. 《Programming WPF》翻译 第3章 1.什么是控件

    原文:<Programming WPF>翻译 第3章 1.什么是控件 对于一个应用程序而言,控件是搭建用户界面的积木.它们具备交互式的特征,例如文本框.按钮以及列表框.尽管如此,WPF还有 ...

  8. WPF 模仿 UltraEdit 文件查看器系列一 用户控件

    WPF 模仿 UltraEdit 文件查看器系列一 用户控件 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 章节: 起步 添加用户控件 ...

  9. WPF通过不透明蒙板切割显示子控件

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/Backspace110/article/ ...

随机推荐

  1. P2085 最小函数值 (堆)

    题目描述 有n个函数,分别为F1,F2,...,Fn.定义Fi(x)=Aix^2+Bix+Ci (x∈N*).给定这些Ai.Bi和Ci,请求出所有函数的所有函数值中最小的m个(如有重复的要输出多个). ...

  2. 【DFS序+树状数组】BNUOJ 52733 Random Numbers

    http://acm.bnu.edu.cn/v3/problem_show.php?pid=52733 [题意] 给定一棵树,这棵树每个点都有一个点权,标号从0开始,0是根结点 修改操作: SEED ...

  3. 转载:c++打印日志文件的一个模板

    转载地址:http://blog.csdn.net/huangyifei_1111/article/details/52134914 NetDataLog.h #ifndef NETDATALOG_H ...

  4. Spring JdbcTemplate操作小结

    Spring 提供了JdbcTemplate 来封装数据库jdbc操作细节: 包括: 数据库连接[打开/关闭] ,异常转义 ,SQL执行 ,查询结果的转换 使用模板方式封装 jdbc数据库操作-固定流 ...

  5. Goldbach

    Description: Goldbach's conjecture is one of the oldest and best-known unsolved problems in number t ...

  6. PatentTips - Write Combining Buffer for Sequentially Addressed Partial Line Operations

    SUMMARY OF THE INVENTION The present invention pertains to a write combining buffer for use in a mic ...

  7. R语言入门视频笔记--3-1--矩阵与数组

    生成一个新矩阵,多用一些参数吧这次: x <- c(12,13,14,15) rname <- c("R1","R2") nname <- c ...

  8. 使用Sharesdk实现第三方平台登录(qq,新浪微博)

    首先到sharesdk开放píng台下载demo ,以下要用到的文件来自于 simple里面 第一步:导入官方的jar包    第二步:添加ShareSDK.xml文件并修改相关píng台key  第 ...

  9. codeforces 1051 F

    多源点最短路. 但是有限制,m - n <= 20,边数 - 点数 <= 20, 所以这个图非常的稀疏. 任意提取出一个生成树出来,LCA处理任意两点最短路. 然后再去遍历那些多余出来的点 ...

  10. Codeforces 848B Rooter's Song(分类+模拟)

    题目链接 Rooter's Song 题意  有n个舞者站在x轴上或y轴上,每个人有不同的出发时间.x轴上的舞者垂直x轴正方向移动,y轴上的舞者垂直y轴正方向移动. 当x轴的舞者和y轴的舞者相遇时,他 ...