这可能听起来像一个奇怪的请求,我不确定这是否真的可能,但我有一个Silverlight DataPager控件,它显示“Page1 of X”,我想更改“Page1 of X”文本以表示不同的内容。
这可以做到吗?
发布于 2012-10-18 19:44:32
在页面样式中,有一个名为CurrentPagePrefixTextBlock的部件,默认情况下,它的值是“DataPager”。你可以参考http://msdn.microsoft.com/en-us/library/dd894495(v=vs.95).aspx了解更多信息。
其中一种解决方案是扩展DataPager
下面是执行此操作的代码
public class CustomDataPager:DataPager
{
public static readonly DependencyProperty NewTextProperty = DependencyProperty.Register(
"NewText",
typeof(string),
typeof(CustomDataPager),
new PropertyMetadata(OnNewTextPropertyChanged));
private static void OnNewTextPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var newValue = (string)e.NewValue;
if ((sender as CustomDataPager).CustomCurrentPagePrefixTextBlock != null)
{
(sender as CustomDataPager).CustomCurrentPagePrefixTextBlock.Text = newValue;
}
}
public string NewText
{
get { return (string)GetValue(NewTextProperty); }
set { SetValue(NewTextProperty, value); }
}
private TextBlock _customCurrentPagePrefixTextBlock;
internal TextBlock CustomCurrentPagePrefixTextBlock
{
get
{
return _customCurrentPagePrefixTextBlock;
}
private set
{
_customCurrentPagePrefixTextBlock = value;
}
}
public CustomDataPager()
{
this.DefaultStyleKey = typeof(DataPager);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
CustomCurrentPagePrefixTextBlock = GetTemplateChild("CurrentPagePrefixTextBlock") as TextBlock;
if (NewText != null)
{
CustomCurrentPagePrefixTextBlock.Text = NewText;
}
}
}现在,通过在这个页面中设置NewText属性,我们可以获得我们想要的任何文本,而不是“CustomDataPager”
Xmlns:local=“clr-命名空间:包含CustomDataPager的程序集”
<local:CustomDataPager x:Name="dataPager1"
PageSize="5"
AutoEllipsis="True"
NumericButtonCount="3"
DisplayMode="PreviousNext"
IsTotalItemCountFixed="True" NewText="My Text" />现在它显示“我的文本”而不是“页面”。
但其他部分也需要定制,以使其正确工作!!
我希望这能回答你的问题
https://stackoverflow.com/questions/12952724
复制相似问题