分享

WPF中如何使用代码操作数据模板生成的控件

 Jcstone 2013-07-03

WPF中如何使用代码操作数据模板生成的控件

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

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客<Border Margin="15" BorderBrush="Aqua" BorderThickness="2" Padding="8" CornerRadius="5">WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客      <StackPanel>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客        <ListBox Name="myListBox" ItemTemplate="{StaticResource myDataTemplate}"

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客                  IsSynchronizedWithCurrentItem="True">

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客          <ListBox.ItemsSource>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客            <Binding Source="{StaticResource InventoryData}" XPath="Books/Book"/>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客          </ListBox.ItemsSource>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客        </ListBox>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客        <Button Margin="10"

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客                 Click="DataTemplateFindElement">Get text of textBlock in DataTemplate</Button>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客      </StackPanel>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客    </Border>

Listbox使用的数据模板如下: 

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客<DataTemplate x:Key="myDataTemplate">

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客      <TextBlock Name="textBlock" FontSize="14">

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客        <TextBlock.Text>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客          <Binding XPath="Title"/>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客        </TextBlock.Text>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客      </TextBlock>

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客    </DataTemplate>

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

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客ListBoxItem myListBoxItem = (ListBoxItem)(myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;    

TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter);

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客MessageBox.Show("The text of the named TextBlock in the DataTemplate of the selected list item: " + myTextBlock.Text);

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

WPF中如何使用代码操作数据模板生成的控件 - 幸运王子 - 幸运王子-刘利民博客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;

     }


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多