我在许多控件中使用以下DataTemplate:
<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>
<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>
</pages:BasePageManageItems.Resources>
有没有办法从XAML使用XamlReader,这样我就可以简单地将DataTemplate的 text 动态加载到XAML文件中?我想象的是这样的东西(伪代码):
<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>
<XamlReader Load="XamlBlocks/DateTemplateManageButtons.xaml"/>
</pages:BasePageManageItems.Resources>
答案 0 :(得分:1)
你不应该将XamlReader标签放在Xaml中(我甚至不知道是否可能)。相反,您可以使用XamlReader类在代码中创建已编译的Xaml,并将其附加到父元素:
var element = XamlReader.Load(stringContainingXaml);
this.somePanel.Children.Insert(0, element as FrameworkElement);
答案 1 :(得分:1)
您可以将公共XAML放在ResourceDictionary中:
XamlBlocks / DateTemplateManageButtons.xaml(添加到项目中,构建动作=页面)
<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);
}