【发布时间】:2009-11-26 12:31:32
【问题描述】:
我有很多这样的用户控件:
PageManageCustomers.xaml.cs:
public partial class PageManageCustomers : BasePage
{
...
}
继承自:
PageBase.cs:
public class BasePage : UserControl, INotifyPropertyChanged
{
...
}
由于 PageBase.cs 没有没有随附的 XAML 文件,我必须将它引用的 XAML 放在 每个 用户控件中继承它,例如以下块在继承PageBase 的每个控件的每个 XAML 文件中重复:
<DataTemplate x:Key="manageAreaCellTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Delete" MouseDown="System_Delete_Click"/>
<TextBlock Text=" "/>
<TextBlock Style="{DynamicResource ManageLinkStyle}"
Tag="{Binding Id}" Text="Edit" MouseDown="System_Edit_Click"/>
</StackPanel>
</DataTemplate>
我正在尝试将此块放入 资源文件,但语法不正确,它说:
'ResourceDictionary' 根元素 需要一个 x:Class 属性来 支持 XAML 中的事件处理程序 文件。删除事件处理程序 对于 MouseDown 事件,或添加一个 x:根元素的类属性。
或者也许我可以用 XamlReader` 以某种方式读取这些块?
我怎样才能把这个重复的代码块放在一个地方,这样它就不会在每个继承 BagePage 的 XAML 文件中重复?
这里是这个问题的一个可重现的例子:
Window1.xaml:
<Window x:Class="TestXamlPage8283.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel x:Name="MainContent"/>
</Window>
Window1.xaml.cs:
using System.Windows;
namespace TestXamlPage8283
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Page1 page1 = new Page1();
MainContent.Children.Add(page1);
Page2 page2 = new Page2();
MainContent.Children.Add(page2);
}
}
}
Page1.xaml:
<local:BasePage x:Class="TestXamlPage8283.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestXamlPage8283"
Height="40" Width="300">
<StackPanel>
<TextBlock Text="{Binding PageTitle}"
FontSize="14"
FontWeight="Bold"/>
<TextBlock Text="This is XAML that is specific to page one." />
</StackPanel>
</local:BasePage>
Page1.xaml.cs:
namespace TestXamlPage8283
{
public partial class Page1 : BasePage
{
public Page1()
{
InitializeComponent();
PageTitle = "Page One";
}
}
}
Page2.xaml:
<local:BasePage x:Class="TestXamlPage8283.Page2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestXamlPage8283"
Height="40" Width="300">
<StackPanel>
<TextBlock Text="{Binding PageTitle}"
FontSize="14"
FontWeight="Bold"/>
<TextBlock Text="This is XAML that is specific to page two." />
</StackPanel>
</local:BasePage>
Page2.xaml.cs:
namespace TestXamlPage8283
{
public partial class Page2 : BasePage
{
public Page2()
{
InitializeComponent();
PageTitle = "Page Two";
}
}
}
BasePage.cs:
using System.Windows.Controls;
using System.ComponentModel;
namespace TestXamlPage8283
{
public class BasePage : UserControl, INotifyPropertyChanged
{
#region ViewModelProperty: PageTitle
private string _pageTitle;
public string PageTitle
{
get
{
return _pageTitle;
}
set
{
_pageTitle = value;
OnPropertyChanged("PageTitle");
}
}
#endregion
public BasePage()
{
DataContext = this;
}
#region INotifiedProperty Block
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
我该如何处理这个块
<TextBlock Text="{Binding PageTitle}"
FontSize="14"
FontWeight="Bold"/>
out 的 Page1.xaml 和 Page2.xaml 并将其放在 一个 位置,以便我可以从 Page1 引用 .xaml 和 Page2.xaml?(这样当我想将 FontSize=14 更改为 FontSize=16 时,我只需在一处更改即可)
【问题讨论】:
-
看来您要问的内容类似于 Asp.net 的母版页。我不知道这是否可能。对于您的 TextBlock 最后一个示例(更改 PageTitle 文本块的 FontSize),这不是样式的用途吗?