WPF - 从自定义ListBoxItem检索子DataTemplate控件 [英] WPF - Retrieve Child DataTemplate Control From Custom ListBoxItem

查看:1073
本文介绍了WPF - 从自定义ListBoxItem检索子DataTemplate控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个ListBox与自定义DataTemplate。 XAML如下:

In my application, I have a ListBox with a custom DataTemplate. The XAML is as follows:

<ListBox Name="lstTasks">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" VerticalAlignment="Top">
                <CheckBox Name="chkIsChecked" VerticalAlignment="Top" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"/>
                <RichTextBox Name="rtbTask" HorizontalAlignment="Left" Height="50" VerticalAlignment="Top" Width="450"
                             BorderBrush="{DynamicResource {x:Static SystemColors.ActiveCaptionTextBrushKey}}" Margin="0,0,0,10"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我使用一个名为CheckBoxItem的自定义类作为我的ListBoxItems。如果我想添加一个项目到ListBox,我将使用下面的代码作为一个例子:

I use a custom class called CheckBoxItem as my ListBoxItems. If I wanted to add an item to the ListBox, I'd use the following code as an example:

lstTasks.Items.Add(new CheckBoxItem("", false));

如果我想从ListBox检索这个项目,我会使用下面的代码: p>

If I wanted to retrieve this item from the ListBox, I'd use the following code:

CheckBoxItem CheckBoxItem = (CheckBoxItem)lstTasks.Items[0];

我对数据绑定没有问题;它工作正常。我的问题是检索DataTemplate中的一个孩子。在应用程序中,用户可以编辑每个CheckBoxItem中的RichTextBox的内容。我想能够将RichTextBox的内容保存到外部文件。我已经创建了获取RTF文本并将其存储在String中的方法,但我似乎无法访问任何给定的CheckBoxItem的RichTextBox。

I have no problem with data binding; it works fine. My issue is with retrieving one of the children in the DataTemplate. In the application, users can edit the contents of the RichTextBox within each CheckBoxItem. I want to be able to save the contents of the RichTextBox to an external file. I've already created methods for getting the RTF text and storing it in a String, but I can't seem to get access to the RichTextBox for any given CheckBoxItem.

我在网上搜索了一段时间,但似乎找不到任何帮助。使用VisualTree不工作,因为CheckBoxItems不是DependencyObjects,不能强制转换到它们。因此,我很抱歉,非常感谢任何洞察这个问题。

I've searched online for quite some time but couldn't seem to find anything that helped. Using the VisualTree does not work because CheckBoxItems are not DependencyObjects and cannot be casted to them. As a result, I'm pretty stuck and would very much appreciate any insight on this issue.

我的一个想法是跟踪所有的RichTextBoxes集合的某种形式。我会处理他们的Loaded事件,然后将他们添加到那里的集合,但我真的不喜欢这样做,因为它似乎是一个不必要的使用内存。

One idea I had was to keep track of all the RichTextBoxes in a collection of some sort. I'd handle their Loaded event and then add them to the collection there, but I'd really prefer not to do this, as it seems like it's an unnecessary use of memory.

先感谢任何帮助!

推荐答案

没有简单的方法。但是,知道你已经来到 VisualTree 之前,你可能已经看到下面的例子中的大多数代码。可能你错过的是第一行,它显示了如何从 CheckBoxItem 中获取 ListBoxItem 。现在有 ListBoxItem ,表示您有 DependencyObject 开始使用 VisualTree stuff技术找到 RichTextBox

There is no easy way for that. But knowing that you already came accross VisualTree stuff before maybe you have seen most of the codes in following example. And possibly what you missed is the first line, that shows you how to get ListBoxItem from CheckBoxItem. Having ListBoxItem on hand, means you have a DependencyObject to start using that VisualTree stuff technique to find RichTextBox :

var myListBoxItem = (lstTasks.ItemContainerGenerator.ContainerFromItem(lstTasks.Items[0]));

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding richtextbox from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
RichTextBox myTextBlock = (RichTextBox)myDataTemplate.FindName("rtbTask", myContentPresenter);

并执行 FindVisualChild a href =http://msdn.microsoft.com/en-us/library/bb613579%28v=vs.110%29.aspx =nofollow> MSDN :

And implementation of FindVisualChild as shown in MSDN :

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 - 从自定义ListBoxItem检索子DataTemplate控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆