将事件绑定/连接到嵌套在 ItemsControls DataTemplate 内的 ItemsControl 中的按钮
我的问题是,当我为按钮连接事件(.. Click =“dummyfunc”)并尝试运行该项目时,我收到未处理的 XamlParseException (4004)并表示“无法分配给属性'System.Windows” .Controls.Primitives.ButtonBase.Click'" 我在页面的 CodeBehind 中声明了我的事件处理程序,其中外部 ItemsControl 放置在 Xaml 中。对于放置在外部控件 DataTemplate 中的按钮来说,它工作得很好。但在内部控件模板中,我无法连接任何事件。
有一点是有效的:
<HyperlinkButton Content="x">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="DeleteMe" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
但这只会触发一个在我上面提到的项目中实现的方法(DeleteMe),该方法具有一些属性和一个集合。
但我需要用通常的方法处理内部控件中的事件:(
public void dummyfunc(object sender, RoutedEventArgs e){...}
这样我就可以获得触发事件的按钮,例如:
sender as HyperinkButton
或者
e.OriginalSource as HyperlinkButton
我认为问题是事件没有冒泡,我得到解析异常,因为解析器无法在实际范围中找到事件处理程序声明,这显然不是内部控件的 CodeBehind :(
我需要这个的原因是因为我想在代码中执行一些自定义 UI 逻辑,并且仅在此之后以某种方式触发DeleteMe 逻辑。
谢谢, 巴林特
My question is very similiar to this: (Binding events to buttons in an ItemsControl) but I didn't found a solution there.
I have an ItemsControl and inside its DataTemplate, I have another ItemsControl. The items in the outer control contain classes that have some properties, and one of the properties is a collection. The inner ItemsControl source is binded to this collection, and inside the inner controls DataTemplate there is a Button.
My problem is that when I wire up an event for the button (.. Click="dummyfunc") and try to run the project, I get an unhandled XamlParseException (4004) and says that "Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click'"
I declared my event handler in the CodeBehind of the page in which the outer ItemsControl is placed in the Xaml. And it works fine for the buttons placed in the outer controls DataTemplate. But in the inner controls template, I just cant wire up any events.
One thing works:
<HyperlinkButton Content="x">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="DeleteMe" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</HyperlinkButton>
But this triggers only a method (DeleteMe) which is implemented in the item I mentioned above, that has some properties and a collection.
But instead of this, I need to handle the events in the inner control with a usual method:
public void dummyfunc(object sender, RoutedEventArgs e){...}
(so i can get the button that fired the event for example:
sender as HyperinkButton
or
e.OriginalSource as HyperlinkButton
I suppose the problem is that the event is not bubbled up and I get the parse exception because the parser cannot find the event handler declaration in the actual scope, which apparently is not the CodeBehind for the inner control :(
The reason I need this, is because I would like to do some custom UI logic in code, and trigger the DeleteMe logic somehow only after this.
Thanks,
Bálint
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从
的斯巴达文档来看,这一点并不明显CallMethodAction
,但由MethodName
属性命名的方法可以具有以下任一签名:void Method()
void Method(object AssociatedObject, object argument)
此外,
EventTrigger
操作的默认参数
是事件参数值,通常作为第二个参数传递给一个事件处理程序。对于Click
事件,参数的类型为RoulatedEventArgs
,因此您可以像这样定义DeleteMe
方法:这使您可以访问所有处理
Click
事件时所需的信息。It's not obvious from the spartan documentation for
CallMethodAction
, but the method named by theMethodName
property can have either of these signatures:void Method()
void Method(object associatedObject, object parameter)
Furthermore, the default
parameter
for anEventTrigger
action is the event arguments value that would normally be passed as the second argument to an event handler. For theClick
event, the type of the parameter isRoutedEventArgs
so you can define yourDeleteMe
method like this:This gives you access to all the information you desire when handling the
Click
event.