简体   繁体   English

当元素位于DataTemplate中时,如何在WPF中生成多个计时器?

[英]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.

相关问题 如何在WPF中访问列表框中的数据模板中的控件 - How Can I Access to The Controls in DataTemplate in ListBox in WPF 如何使用DataTemplate实现对WPF ListBox的搜索? - How can I implement search for a WPF ListBox with DataTemplate? 如何在WPF中查找由ListViewItem DataTemplate生成的元素 - how to find elements that are generated by a ListViewItem DataTemplate in WPF 如何在ListView的DataTemplate中添加其他元素? - How can I add additional elements to a DataTemplate in a ListView? 如何为WPF TreeView设置DataTemplate以显示列表的所有元素? - How do I set a DataTemplate for a WPF TreeView to display all Elements of an List? 当我 select 一个项目时,如何在数据模板中切换组件? - How can I toggle component in datatemplate, when I select an item? WPF-DataTemplate属性内的元素在没有绑定时出现问题? - WPF - Elements inside DataTemplate property issue when no binding? 我正在使用WPF,我有DataTemplate,我想访问如何使用此代码的代码? - I am using WPF and I have DataTemplate that is i want to access into the codebehind how I can use this? WPF中的网格面板元素是否有数据窗口? - is there a datatemplate for grid panel elements in WPF? 如何访问 WPF 代码隐藏中 ContentControl DataTemplate 中的 object,或者如何在 xaml 中设置 CefSharp DownloadHandler - How can I access an object that is in a ContentControl DataTemplate in WPF codebehind, or how do I set a CefSharp DownloadHandler in xaml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM