[英]How can I generate multiple timers in WPF when elements are in a DataTemplate?
How can I manage to access objects in Code Behind from Window.Resources? 如何管理从Window.Resources中的代码隐藏对象?
Follow-up: 跟进:
Actually, I'm trying to generate multiple timers. 实际上,我正在尝试生成多个计时器。 How can I properly to do it in WPF if my elements are inside a DataTemplate?
如果我的元素位于DataTemplate中,如何在WPF中正确执行此操作?
To find control inside template/datatemplate use FindName method. 要在模板/数据模板中查找控件,请使用FindName方法。
http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx
EDIT: 编辑:
Or you could use class and TwoWay binding to set text or whatever you need. 或者,您可以使用类和TwoWay绑定来设置文本或所需的任何内容。
class MyCustomTimer : INotifyPropertyChanged
{
string title { get; set; }
DispatcherTimer timer { get; set; }
...
}
Bind in template with TwoWay binding: 使用TwoWay绑定绑定到模板中:
<TextBox Text={Binding title, Mode=TwoWay} />
Add class to listview: 将类添加到列表视图:
this.listview.items.add(new MyCustomTimer(...));
And then access values in datatemplate like: 然后访问datatemplate中的值,例如:
MyCustomTimer item0 = this.listview.Items[0] as MyCustomTimer;
item0.title = "This is text";
As i can think for it. 正如我能想到的。 no you can't access the elements inside aa datatemplate.
不,您不能访问数据模板中的元素。 A DataTemplate is just a representation /xaml view of the managed object.
DataTemplate只是托管对象的表示形式/ xaml视图。 so if you have something which is useful to you.
因此,如果您有对您有用的东西。 I recommend you to use the Custom control and create some TemplateParts for it.
我建议您使用“ 自定义”控件并为其创建一些TemplateParts 。 by this way you can get the template part from the code .
通过这种方式,您可以从代码中获取模板部分 。
Other thing you can do is use the VisualTreehelper class this will give you the parents and childs from the elements. 您可以做的另一件事是使用VisualTreehelper类,这将为您提供来自元素的父级和子级。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.