私はListViewコントロールを持つDataTemplateを持っています。このDataTemplateはTemplates.xaml(ResourceDictionary)にあります。 Template.xamlは、ResourceDictionary.MergedDictionariesを使用してメインのUserControl SourceManager.xamlに含まれます。私はDataTemplateのListViewのSelectionChangedイベントを発生させたいが、コード内のハンドラをSourceManager.xaml.csに入れたい。WPF:別のファイルにあるDataTemplateのイベントを処理するにはどうすればよいですか?
どうすれば実現できますか?
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 [email protected]}" />
<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 [email protected]}" 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>
あなたの応答に感謝しますが、このソリューションは、すべてのセレクタ派生クラスがこのハンドラに当たっているので少し面倒です。別の方法がありますか? – Mike
ResourceDictionaryにコードビハインドを追加し、UserControlのようにイベントハンドラを追加することができます。これを行うと、イベントハンドラはResourceDictionaryのコードビハインドで呼び出され、UserControlでは呼び出されません。 –
ええ、私は、UserControlのコードの背後にあるハンドラが必要...それはこの質問の理由のすべてです。 – Mike