[英]Binding to ContextMenuOpening in DataTemplate within ResourceDictionary XAML
I have a ResourceDictionary that contains a <DataTemplate>
with a <TextBox>
in it. 我有一个ResourceDictionary,其中包含带有
<TextBox>
的<DataTemplate>
。 The problem is how to use a binding to connect the ContextMenuOpening
event of the TextBox
. 问题是如何使用绑定来连接
TextBox
的ContextMenuOpening
事件。 I have tried creating a DependencyProperty through DependencyProperty.Register
with a name that matches the Binding in the ContextMenuOpening
event, but at runtime the error is: 我尝试通过
DependencyProperty.Register
创建一个DependencyProperty,其名称与ContextMenuOpening
事件中的Binding相匹配,但在运行时错误为:
A 'Binding' cannot be set on the 'AddContextMenuOpeningHandler' property of type 'TextBox'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
ResourceDictionary XAML: ResourceDictionary XAML:
<DataTemplate>
<Grid>
<TextBox ContextMenuOpening="{Binding ??WHAT GOES HERE?? }" />
</Grid>
</DataTemplate>
Is what I'm trying to do even possible because of the XAML being in the ResourceDictionary rather than in the XAML of a UserControl? 由于XAML在ResourceDictionary中,而不在UserControl的XAML中,我试图做的事情甚至可能吗?
Is what I'm trying to do even possible because of the XAML being in the ResourceDictionary rather than in the XAML of a UserControl?
由于XAML在ResourceDictionary中,而不在UserControl的XAML中,我试图做的事情甚至可能吗?
Yes, you could add a code-behind file to a ResourceDictionary as described here: 是的,您可以按照以下说明将代码隐藏文件添加到ResourceDictionary:
Is it possible to set code behind a resource dictionary in WPF for event handling? 是否可以在WPF中的资源字典后面设置代码以进行事件处理?
Once you have added the code-behind file you could handle the event as usual: 一旦添加了代码隐藏文件,便可以照常处理事件:
<TextBox ContextMenuOpening="TextBox_ContextMenuOpening" />
private void TextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
//do your thing...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.