Change DataPager text

不羁的心 提交于 2019-12-11 20:18:40

问题


This might sound like a strange request and i'm not sure if it's actually possible, but I have a Silverlight DataPager control where it says "Page 1 of X" and I want to change the "Page" text to say something different.

Can this be done?


回答1:


In DataPager style there is a part by name CurrentPagePrefixTextBlock by default its value is "Page". You can refer http://msdn.microsoft.com/en-us/library/dd894495(v=vs.95).aspx for more info.

One of the solution is to extend DataPager

Here is the code to do that

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;
        }
    }  

}

Now by setting NewText property in this CustomDataPager we can get whatever text we want instead of "Page"

xmlns:local="clr-namespace:Assembly which contains CustomDataPager"

<local:CustomDataPager x:Name="dataPager1" 
                        PageSize="5"
                        AutoEllipsis="True"
                        NumericButtonCount="3"
                        DisplayMode="PreviousNext"
                        IsTotalItemCountFixed="True" NewText="My Text" />

Now it displays "My Text" Instead of "Page".
But other parts also need to be customised inorder make this work correctly!!
Hope this answers your question



来源:https://stackoverflow.com/questions/12952724/change-datapager-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!