访问 WPF 中 ListBox 中 DataTemplate 中的控件

发布于 2024-12-17 09:45:23 字数 360 浏览 0 评论 0原文

我有包含 DataTemplate 的列表框。 我无法访问放置在数据模板中的控件。 我怎样才能访问这个控件?

<ListBox Height="344" Name="listBoxMedicine" Width="881">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <TextBlock Name="myTextBlock">
        </Datatemplate>
    </ListBox.ItemTemplate>
</ListBox>

感谢您的关注。

I have listbox which have DataTemplate.
I can not access controls which placed in the datatemplate.
How can I access to this controls?

<ListBox Height="344" Name="listBoxMedicine" Width="881">
    <ListBox.ItemTemplate>
        <DataTemplate >
            <TextBlock Name="myTextBlock">
        </Datatemplate>
    </ListBox.ItemTemplate>
</ListBox>

Thank you for your attention.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

徒留西风 2024-12-24 09:45:23

如果您仍然想在代码隐藏中访问控件,您可以执行以下操作:

1)在某处添加新的辅助方法:

    public static IEnumerable<Visual> ToVisualTree(this Visual visual)
    {
        yield return visual;

        int numVisuals = VisualTreeHelper.GetChildrenCount(visual);
        for (int i = 0; i < numVisuals; ++i)
        {
            var child = (Visual)VisualTreeHelper.GetChild(visual, i);

            if (child == null) yield break;

            foreach (var subItem in child.ToVisualTree())
            {
                yield return subItem;
            }                
        }
    }

2)像这样使用它:

 var allTextBlocks = listBoxMedicine.ToVisualTree().OfType<TextBlock>().ToList();

但我仍然强烈建议重构您的数据模型。

If you still want access your controls in codebehaind, you can do something like this:

1) Add a new helper method somewhere:

    public static IEnumerable<Visual> ToVisualTree(this Visual visual)
    {
        yield return visual;

        int numVisuals = VisualTreeHelper.GetChildrenCount(visual);
        for (int i = 0; i < numVisuals; ++i)
        {
            var child = (Visual)VisualTreeHelper.GetChild(visual, i);

            if (child == null) yield break;

            foreach (var subItem in child.ToVisualTree())
            {
                yield return subItem;
            }                
        }
    }

2) Use it like this:

 var allTextBlocks = listBoxMedicine.ToVisualTree().OfType<TextBlock>().ToList();

But I still strongly recomend to refactor your data model.

过潦 2024-12-24 09:45:23

根据评论,我建议您创建一个视图模型,它只提供可见性的属性,例如:

public class DataViewModel : INotifyPropertyChanged
{
    private Data _data;
    // Some data property.
    public Data Data { get { return _data; } set { ... } }

    private Visibility _visibility;
    // The visibility property.
    public Visibility Visibility { get { return _visibility; } set { ... } }
}

然后您可以绑定该可见性,然后在代码中设置它以影响视图:

<DataTemplate >
    <TextBlock Name="myTextBlock" Visibility="{Binding Visibility}">
</Datatemplate>

Based on the comments i would suggest you create a view-model which simply provides a property for the visbility, e.g.:

public class DataViewModel : INotifyPropertyChanged
{
    private Data _data;
    // Some data property.
    public Data Data { get { return _data; } set { ... } }

    private Visibility _visibility;
    // The visibility property.
    public Visibility Visibility { get { return _visibility; } set { ... } }
}

You can then bind that visibility and later set it in code to affect the view:

<DataTemplate >
    <TextBlock Name="myTextBlock" Visibility="{Binding Visibility}">
</Datatemplate>
浅暮の光 2024-12-24 09:45:23

我使用这种方法从 ItemsControl 获取 FrameworkElement,也可以使用 ListBox、ListView,因为它们都继承自 ItemsControl。

private void CheckBounds(ItemsControl itemsControl)
    {
        foreach (var item in itemsControl.Items)
        {
            var child = ((FrameworkElement)itemsControl.ItemContainerGenerator.ContainerFromItem(item));
            child.IsEnabled = child.IsControlVisible(itemsControl);
        }
    }

I'm using this approach to get FrameworkElement from ItemsControl, also will work with ListBox, ListView because they all inherit from ItemsControl.

private void CheckBounds(ItemsControl itemsControl)
    {
        foreach (var item in itemsControl.Items)
        {
            var child = ((FrameworkElement)itemsControl.ItemContainerGenerator.ContainerFromItem(item));
            child.IsEnabled = child.IsControlVisible(itemsControl);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文