是否有可能将绑定在Silverlight的DataTemplate事件? [英] Is it possible to bind an Event in a Silverlight DataTemplate?

查看:162
本文介绍了是否有可能将绑定在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事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆