gpt4 book ai didi

WPF ListBox - 获取 UIElement 而不是 SelectedItem

转载 作者:行者123 更新时间:2023-12-03 23:34:42 25 4
gpt4 key购买 nike

我创建了一个 ListBox有一个 DataTemplateItemtemplate .但是,有没有一种简单的方法可以访问生成的 UIElement而不是 SelectedItem在代码隐藏中?

当我访问 SelectedItem ,我只是从我的ItemsSource收藏。有没有办法访问 UIElement (即。
DataTemplate 生成的元素连同绑定(bind)的对象)?

最佳答案

大小,安迪和博德克是绝对正确的。

以下是我如何能够使用其句柄检索列表框所选项目的文本框。

var container = listboxSaveList.ItemContainerGenerator.ContainerFromItem(listboxSaveList.SelectedItem) as FrameworkElement;
if (container != null)
{
ContentPresenter queueListBoxItemCP = VisualTreeWalker.FindVisualChild<ContentPresenter>(container);
if (queueListBoxItemCP == null)
return;

DataTemplate dataTemplate = queueListBoxItemCP.ContentTemplate;

TextBox tbxTitle = (TextBox)dataTemplate.FindName("tbxTitle", queueListBoxItemCP);
tbxTitle.Focus();
}

(注意:在这里,VisualTreeWalker 是我自己对 VisualTreeHelper 的包装器,其中暴露了各种有用的功能)

关于WPF ListBox - 获取 UIElement 而不是 SelectedItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/610343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com