Вы можете поместить общий XAML в ResourceDictionary:
XamlBlocks / DateTemplateManageButtons.xaml (добавлено в проект, действие сборки = Page)
<ResourceDictionary x:Class="myNmaespace.DateTemplateManageButtons"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DataTemplate x:Key="manageAreaCellTemplate">
<Border Padding="2">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Add" MouseDown="System_Add_Click"
Margin="0 0 5 0"/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Copy" MouseDown="System_Copy_Click"
Margin="0 0 5 0"/>
</StackPanel>
</Border>
</DataTemplate>
</ResourceDictionary>
XamlBlocks / DateTemplateManageButtons.xaml.cs:
namespace myNamespace
{
public partial class DateTemplateManageButtons : ResourceDictionary
{
private void System_Delete_Click(object sender, RoutedEventArgs e)
{
// event handler code
}
// other event handlers
}
}
А на вашей странице:
<pages:BasePageManageItems x:Class="TestApp.Pages.PageManageAddresses"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:pages="clr-namespace:TestHistorierung.Pages"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Background="#eee"
VerticalAlignment="Stretch">
<pages:BasePageManageItems.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="XamlBlocks/DateTemplateManageButtons.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</pages:BasePageManageItems.Resources>
Если вам нужен код обработчика событий для запуска на странице, а не словарь ресурсов, вы можете сделать что-то вроде:
Определение интерфейса для событий:
public interface IDateTemplateManageButtonsEvents
{
void System_Delete_Click(object sender, RoutedEventArgs e);
}
Реализация этого интерфейса на всех страницах, где используется шаблон данных
В словаре ресурсов cs файла:
private IDateTemplateManageButtonsEvents FindPage(object sender)
{
DependencyObject current = sender as DependencyObject;
while(current != null && !(current is IDateTemplateManageButtonsEvents))
{
current = VisualTreeHelper.GetParent(current);
}
return (IDateTemplateManageButtonsEvents)current;
}
private void System_Delete_Click(object sender, RoutedEventArgs e)
{
FindPage(sender).System_Delete_Click(sender, e);
}