需要帮助处理Application.xaml文件中DataTemplate的事件 [英] Need help handling events of a DataTemplate in the Application.xaml file

查看:165
本文介绍了需要帮助处理Application.xaml文件中DataTemplate的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中有一个数据模板,其中有几个按钮。
我希望那些按钮在当前页面被触发(我在许多页面中使用这个模板),而不是在Application.xaml.vb / cs文件中,因为我想要在每个页面上执行不同的操作。



我希望我清楚。

解决方案

实现这一点。在 DataTemplate 中执行按钮执行特定的命令 s :

 < Button Command ={x:Static MyCommands.SomeCommand}/> 

然后使每个视图使用 DataTemplate 处理命令

 < UserControl> 
< UserCommand.CommandBindings>
< CommandBinding Command ={x:Static MyCommands.SomeCommand}
Executed =_ someHandler/>
< /UserCommand.CommandBindings>
< / UserControl>

评论后编辑:为您的代码创建代码 ResourceDictionary 根据这些说明,您可以简单地按照通常的方式连接事件:



MyResources.xaml

 < ListBox x:Key =myListBoxResourceItemSelected = _listBox_ItemSelected/> 

然后在 MyResources.xaml.cs 中:

  private void _listBox_ItemSelected(object sender,EventArgs e)
{
...
}


I have in my application a data template that has a few buttons. I want those buttons' even handler to be fired in the current page (I am using this template in many pages) rather than in the Application.xaml.vb/cs file, since I want different actions on each page.

I hope I am clear.

解决方案

You can use commanding to achieve this. Have the Buttons in the DataTemplate execute specific Commands:

<Button Command="{x:Static MyCommands.SomeCommand}"/>

Then have each view that uses that DataTemplate handle the Command:

<UserControl>
    <UserCommand.CommandBindings>
         <CommandBinding Command="{x:Static MyCommands.SomeCommand}"
                         Executed="_someHandler"/>
    </UserCommand.CommandBindings>
</UserControl>

EDIT after comments: Once you have created a code-behind for your ResourceDictionary as per these instructions, you can simply connect events in the usual fashion:

In MyResources.xaml:

<ListBox x:Key="myListBoxResource" ItemSelected="_listBox_ItemSelected"/>

Then in MyResources.xaml.cs:

private void _listBox_ItemSelected(object sender, EventArgs e)
{
    ...
}

这篇关于需要帮助处理Application.xaml文件中DataTemplate的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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