需要帮助处理Application.xaml文件中DataTemplate的事件 [英] Need help handling events of a DataTemplate in the Application.xaml file
问题描述
我希望那些按钮在当前页面被触发(我在许多页面中使用这个模板),而不是在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 Button
s in the DataTemplate
execute specific Command
s:
<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屋!