是否有可能将绑定在Silverlight的DataTemplate事件?

编程入门 行业动态 更新时间:2024-10-20 05:39:47
本文介绍了是否有可能将绑定在Silverlight的DataTemplate事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否可以绑定在Silverlight的DataTemplate事件?如果有,是什么做的最好方法是什么?

Is it possible to bind an Event in a Silverlight DataTemplate? If so, what is the best way to do it?

例如,假设您已经创建了一个有一个按钮,这样一个DataTemplate:

For example, say you've created a DataTemplate that has a Button in it, like this:

<UserControl.Resources> <DataTemplate x:Key="MyDataTemplate" > <Grid> <Button Content="{Binding ButtonText}" Margin="4" /> </Grid> </DataTemplate> </UserControl.Resources>

然后,将其应用到一个ListBox的ItemTemplate,像这样的:

Then, you apply it to a ListBox ItemTemplate, like this:

<Grid x:Name="LayoutRoot" Background="White"> <ListBox x:Name="lbListBox" ItemTemplate="{StaticResource MyDataTemplate}" /> </Grid>

如果您设定ListBox的的ItemSource到类的对象列表:

If you set the ListBox's ItemSource to a list of objects of the class:

public class MyDataClass { public string ButtonText{ get; set; } }

那么,如何你赶上从每个按钮在列表中点击按钮从DataTemplate中?您可以使用绑定到Click事件的方法绑定在MyButtonClass,像这样的:

How then do you catch the button click from each button from the DataTemplate in the list? Can you use binding to bind the Click event to a method in "MyButtonClass", like this:

<UserControl.Resources> <DataTemplate x:Key="MyDataTemplate" > <Grid> <Button Click="{Binding OnItemButtonClick}" Content="{Binding ButtonText}" Margin="4" /> </Grid> </DataTemplate> </UserControl.Resources>

将这项工作?如果是这样,我应该把在MyDataClass捕捉事件?

Would this work? If so, what should I put in the "MyDataClass" to catch the event?

谢谢,杰夫

推荐答案

有几个选项。

一。使绑定数据对象该行的自定义控件。该自定义控件上添加的处理程序中绑定的对象。

One. make a custom control that is bound the data object for that row. On that custom control add the handler for the bound object.

我不认为你在点击绑定将工作。 SANS绑定说明书,只是声明你点击一个字符串。

I dont think your binding on the click will work. Sans the Binding Statment and just declare your click to a string.

...添加处理程序,其中,控制被收纳在页面上。请记住,如果你这样捆绑你将只能与该项目(按钮)的发件人工作和它的属性。如果您需要在特定的属性得到一个对象,你是他最好追求第一个选项。

Add the handler on the page where the control is housed. Keep in mind that if you bind this way you will only be able to work with the sender of that item (button) and it's properties. If you need to get at specific attributes on an object you maybe better off pursuing the first option.

小例子,通过添加10个按钮与单击事件的列表框演示功能。 HTH

Small Example demonstrating the functionality by adding 10 buttons to a list box with click events. HTH

DataTemplate中的XAML

DataTemplate XAML

<UserControl.Resources> <DataTemplate x:Name="MyDataTemplate"> <Grid> <Button Click="Button_Click" Content="{Binding ItemText}"/> </Grid> </DataTemplate> </UserControl.Resources>

列表框XAML

ListBox XAML

<ListBox x:Name="ListBoxThingee" ItemTemplate="{StaticResource MyDataTemplate}"/>

code的背后(我只是插入到所有这些的文件的Page.xaml

Code Behind (I just plugged this all into the page.xaml file

public class MyClass { public string ItemText { get; set; } } public partial class Page : UserControl { ObservableCollection<MyClass> _Items; public Page() { InitializeComponent(); _Items = new ObservableCollection<MyClass>(); for (int i = 0; i < 10; i++) { _Items.Add(new MyClass() {ItemText= string.Format("Item - {0}", i)}); } this.ListBoxThingee.ItemsSource = _Items; } private void Button_Click(object sender, RoutedEventArgs e) { Button _b = sender as Button; if (_b != null) { string _s = _b.Content as string; MessageBox.Show(_s); } } }

更多推荐

是否有可能将绑定在Silverlight的DataTemplate事件?

本文发布于:2023-11-07 05:36:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565684.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有可能   绑定   事件   Silverlight   DataTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!