WPF - 获取没有事件的 DataTemplate
我在 Windows.Resources 部分中定义了一个 WPF 数据模板。
我想通过后面的代码访问它(实际上是上面的一个列表框)。我该怎么做?
我无法通过事件获得我想要的东西,因为我想在 BackGroundWorker 的已完成步骤中获取引用。 (即没有发送者(即 UI 元素)可以离开那里)。
我怎样才能得到这方面的参考?
我已经尝试过:
myContainingPanel.Template.Find("lstBox", myContainingPanel);
并且(取自此处):
ContentPresenter contentPresenter = myContainingPanel.FindVisualChild<ContentPresenter>();
DataTemplate template = contentPresenter.ContentTemplate;
return (T) template.FindName(name, contentPresenter);
我会接受一个答案说我应该使用 MVVM,如果有一个很好的例子展示它如何工作(使用后台工作人员完成的事件)。
感谢您的帮助
I have a WPF Data template defined in my Windows.Resources section.
I would like to access this (really a list box on it) via the code behind. How can I do this?
I can't get what I want via an event because I want to get the reference in the completed step of a BackGroundWorker. (Ie no sender (that is a UI element) to go off of there).
How can I get a reference to this?
I have tried:
myContainingPanel.Template.Find("lstBox", myContainingPanel);
and (Taken from here):
ContentPresenter contentPresenter = myContainingPanel.FindVisualChild<ContentPresenter>();
DataTemplate template = contentPresenter.ContentTemplate;
return (T) template.FindName(name, contentPresenter);
I would accept an answer that says I should use MVVM if there is an good example showing how that would work (with a backgroundworker completed event).
Thanks for any help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
FrameworkElement.FindResource() 应该可以工作。从视图模型中查找数据模板对我来说似乎并不合适。你想做什么?
FrameworkElement.FindResource() should work. Finding data templates from viewmodel doesn't seem right to me. What are you trying to do?