我有一个带有ListView控件的DataTemplate。此DataTemplate位于Templates.xaml(这是一个ResourceDictionary)。然后,Template.xaml通过ResourceDictionary.MergedDictionaries包含在我的主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>
答案 0 :(得分:0)
由于SelectionChanged
是RoutedEvent
,您可以将其应用于UserControl
,如下所示:
<UserControl ...
ListView.SelectionChanged="MyEventHandler" />
请注意,将为所有 Selector
派生类调用此事件处理程序(因为Selector
是定义和引发事件的位置),它们是您{的后代{1}},其中包括UserControl
,ComboBox
,Menu
等。
答案 1 :(得分:-2)
创建行为
将其放入数据模板中。
就是这样。