将事件绑定/连接到嵌套在 ItemsControls DataTemplate 内的 ItemsControl 中的按钮

发布于 2024-12-12 06:25:08 字数 1466 浏览 0 评论 0原文

我的问题与此非常相似:(将事件绑定到 ItemsControl 中的按钮)但我没有在那里找到解决方案。 我有一个 ItemsControl,在其 DataTemplate 内,我有另一个 ItemsControl。外部控件中的项目包含具有某些属性的类,其中一个属性是集合。内部 ItemsControl 源绑定到此集合,并且内部控件 DataTemplate 内部有一个 Button。

我的问题是,当我为按钮连接事件(.. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请恋爱 2024-12-19 06:25:08

的斯巴达文档来看,这一点并不明显CallMethodAction,但由 MethodName 属性命名的方法可以具有以下任一签名:

  • void Method()
  • void Method(object AssociatedObject, object argument)

此外,EventTrigger 操作的默认参数是事件参数值,通常作为第二个参数传递给一个事件处理程序。对于 Click 事件,参数的类型为 RoulatedEventArgs,因此您可以像这样定义 DeleteMe 方法:

public void DeleteMe(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("sender: {0}", sender);
    Debug.WriteLine("e.OriginalSource: {0}", e.OriginalSource);
}

这使您可以访问所有处理 Click 事件时所需的信息。

It's not obvious from the spartan documentation for CallMethodAction, but the method named by the MethodName property can have either of these signatures:

  • void Method()
  • void Method(object associatedObject, object parameter)

Furthermore, the default parameter for an EventTrigger action is the event arguments value that would normally be passed as the second argument to an event handler. For the Click event, the type of the parameter is RoutedEventArgs so you can define your DeleteMe method like this:

public void DeleteMe(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("sender: {0}", sender);
    Debug.WriteLine("e.OriginalSource: {0}", e.OriginalSource);
}

This gives you access to all the information you desire when handling the Click event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文