是否可以在 Silvelright 中本地化 DataPager 的页脚(Y 页 X)?
这些字符串似乎位于 DataPager 程序集中嵌入的资源中。那么我应该如何本地化呢?
不幸的是,DataPager 类中几乎没有任何东西是虚拟的,而且它使用了许多内部类,因此不可能(至少很容易)继承 DataPager 并覆盖该行为。
是否可以在 Silvelright 中本地化 DataPager 的页脚(Y 页 X)?
这些字符串似乎位于 DataPager 程序集中嵌入的资源中。那么我应该如何本地化呢?
不幸的是,DataPager 类中几乎没有任何东西是虚拟的,而且它使用了许多内部类,因此不可能(至少很容易)继承 DataPager 并覆盖该行为。
这很简单。看看我如何将 DataPager 本地化为葡萄牙语:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MarceloOliveira.Controls
{
/// <summary>
/// Customização feita sobre o Data Pager padrão do Silverlight, para traduzir para o português
/// </summary>
public class CustomDataPager : DataPager
{
TextBlock currentPagePrefixTextBlock;
TextBlock currentPageSuffixTextBlock;
TextBox currentPageTextBox;
public CustomDataPager() : base()
{
this.PageIndexChanged += new EventHandler<EventArgs>(CustomDataPager_PageIndexChanged);
this.MouseLeftButtonDown += new MouseButtonEventHandler(CustomDataPager_MouseLeftButtonDown);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
currentPagePrefixTextBlock = GetTemplateChild("CurrentPagePrefixTextBlock") as TextBlock;
currentPageSuffixTextBlock = GetTemplateChild("CurrentPageSuffixTextBlock") as TextBlock;
currentPageTextBox = GetTemplateChild("CurrentPageTextBox") as TextBox;
currentPageTextBox.TextChanged += new TextChangedEventHandler(currentPageTextBox_TextChanged);
currentPageSuffixTextBlock.SizeChanged += new SizeChangedEventHandler(currentPageSuffixTextBlock_SizeChanged);
}
void currentPageTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TranslateLabels();
}
void CustomDataPager_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TranslateLabels();
}
void CustomDataPager_PageIndexChanged(object sender, EventArgs e)
{
TranslateLabels();
}
void currentPageSuffixTextBlock_SizeChanged(object sender, SizeChangedEventArgs e)
{
TranslateLabels();
}
private void TranslateLabels()
{
if (currentPagePrefixTextBlock != null)
{
currentPagePrefixTextBlock.Text = "Pág.";
currentPageSuffixTextBlock.Text = string.Format("de {0}", this.PageCount);
}
}
}
}
只需将您需要的文化添加到项目文件中的 SupportedCultures 元素中,例如
<SupportedCultures>en,de</SupportedCultures>
现在 DataPager 在德国计算机上使用德国资源。
The only solution I've found out so far is to edit the template of DataPager, remove the two textboxes responsible for displaying "Page" and "of X" and create new ones. Then, inherit DataPager, override OnApplyTemplate to attach to your new TextBoxes.
The last part is the trickiest - you have to handle proper events of your datasource (it depends on the datasource) and update text of your new textboxes.
Although this solution should work, it is not very nice...
另一种选择是删除“后缀”文本块,并通过绑定到 DataPager 的 PageCount 属性添加您自己的文本块:
<TextBlock Text="{Binding PageCount, RelativeSource={RelativeSource TemplatedParent}, StringFormat='/ \{0\}'}" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" />
因此,还有另一种解决方案——更改 DLL 中的资源。
解决方案基于这篇文章。
由于 System.Controls.Data.dll 由 MS 签名,我需要删除签名(强名称)。我使用AdmiralDebilitate将其删除。
通过命令反汇编dll
ildasm /out=System.Controls.Data.il System.Controls.Data.dll
使用任何资源编辑器(我使用Resource.net)打开 System.Windows.Controls.DataPager.PagerResources.resources。
通过命令重新组装程序集
ilasm /resource=System.Controls.Data.res /dll /output=System.Controls.Data.dll System.Controls.Data.il
完毕。
有两个可能的问题: