【问题标题】:Handling events of Application.Resources where they are used处理使用它们的 Application.Resources 的事件
【发布时间】:2011-12-19 10:46:19
【问题描述】:

我有 Button 在 application.resources DataTemplate:

<DataTemplate x:Key="contactItem" DataType="{x:Type Model:Contact}">
<Button x:Name="openButton" Style="{StaticResource openButton}"/>
</DataTemplate>

如何处理openButton点击UserControl

<ItemsControl ItemSource="{Binding CurrentContacts}" ItemTemplate="{StaticResource contactItem}">
</ItemsControl>

【问题讨论】:

    标签: wpf user-controls datatemplate


    【解决方案1】:

    您可以将ButtonBase.Click 处理程序添加到ItemsControl

     <ItemsControl ButtonBase.Click="MyItemsControl_Click">
      </ItemsControl>
    

    在该事件处理程序的代码中,您可以使用e.OriginalSourceDataContext 属性(它应该是Button 类型)来访问数据绑定对象。

    private void MyItemsControl_Click(object sender, RoutedEventArgs e)
    {
        var myDataObject = ((Button)e.OriginalSource).DataContext;
    }
    

    【讨论】:

    • 对,毕竟这就是事件路由的用途。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    相关资源
    最近更新 更多