WPF-获取没有事件的DataTemplate [英] WPF - Get DataTemplate without an event
问题描述
我在Windows.Resources部分中定义了WPF数据模板。
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?
我无法通过事件获得想要的东西,因为我想在BackGroundWorker的完整步骤中获得引用。 (即没有发送者(即UI元素)可以离开那里。)
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).
如何获得对此的引用?
我尝试过:
myContainingPanel.Template.Find("lstBox", myContainingPanel);
和(取自此处):
ContentPresenter contentPresenter = myContainingPanel.FindVisualChild<ContentPresenter>();
DataTemplate template = contentPresenter.ContentTemplate;
return (T) template.FindName(name, contentPresenter);
我会接受一个答案,即如果有一个很好的例子说明应该如何使用MVVM,我应该使用MVVM。
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).
感谢您的帮助
推荐答案
FrameworkElement.FindResource()应该管用。从viewmodel查找数据模板对我来说似乎不合适。您想做什么?
FrameworkElement.FindResource() should work. Finding data templates from viewmodel doesn't seem right to me. What are you trying to do?
这篇关于WPF-获取没有事件的DataTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!