WPF:如何处理单独文件中的DataTemplate中的事件?

WPF:如何处理单独文件中的DataTemplate中的事件?,wpf,events,datatemplate,Wpf,Events,Datatemplate,我有一个带有ListView控件的DataTemplate。此DataTemplate位于Templates.xaml(一个ResourceDictionary)中。然后通过ResourceDictionary.MergedDictionaries将Template.xaml包含到我的主UserControl SourceManager.xaml中。我希望引发DataTemplate的ListView的SelectionChanged事件,但我希望代码隐藏中的处理程序位于SourceManage

我有一个带有ListView控件的DataTemplate。此DataTemplate位于Templates.xaml(一个ResourceDictionary)中。然后通过ResourceDictionary.MergedDictionaries将Template.xaml包含到我的主UserControl SourceManager.xaml中。我希望引发DataTemplate的ListView的SelectionChanged事件,但我希望代码隐藏中的处理程序位于SourceManager.xaml.cs中

我怎样才能做到这一点

Templates.xaml:

<ResourceDictionary x:Class="LawBib.Templates"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<DataTemplate x:Key="SectionTemplate">
    <StackPanel>
        <TextBlock Text="{Binding XPath=@Title}" />
        <ListView x:Name="GroupList" ItemsSource="{Binding XPath=Source}">
            <ListView.Template>
                <ControlTemplate>
                    <WrapPanel IsItemsHost="True">

                    </WrapPanel>
                </ControlTemplate>
            </ListView.Template>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <Image Source="images/source.png" />
                        <TextBlock Text="{Binding XPath=@Title}" HorizontalAlignment="Center" />
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackPanel>
</DataTemplate>

SourceManager.xaml:

<UserControl x:Class="LawBib.SourceManager"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300" Background="#f5f7f8">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources.xaml" />
                <ResourceDictionary Source="Templates.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
...
</UserControl>

...

由于
选择更改
是一个
路由事件
,您可以将其应用于
用户控件
,如下所示:

<UserControl ...
             ListView.SelectionChanged="MyEventHandler" />

请注意,此事件处理程序将为所有
选择器
派生类(因为
选择器
是定义和引发事件的地方)调用,这些派生类是
用户控件的后代,其中包括
组合框
菜单
列表框
,等等。

创建一个行为

将其放入数据模板中


就这样。

谢谢您的回复,但是这个解决方案看起来有点混乱,因为所有选择器派生的类都会碰到这个处理程序。还有其他方法吗?您可以向ResourceDictionary中添加代码,并像在UserControl中一样添加事件处理程序。如果这样做,事件处理程序将在ResourceDictionary的代码隐藏中调用,而不是在UserControl的代码隐藏中调用。是的,但我需要处理程序在UserControl的代码隐藏中。。。这就是这个问题的全部原因。是否有一种简单的方法可以从ResourceDirectory的代码中访问UserControl类实例?我想不出一种方法来解决这个问题。在事件处理程序中的RoutedEventArgs中,您应该能够检查Source属性以获得引发事件的实际控件。从那里你可以确定你是否对它感兴趣。我不认为在这种情况下我需要一种行为。