Skip navigation

In previous post I’ve used Pager class which is contained in my grid controller, and so there is an implementation:

    [NotifyPropertyChanged]
    public class Pager : IPager
    {
        private uint m_CurrentPage;

        private uint m_PageSize;

        private uint m_RecordFrom;

        private uint m_RecordTo;

        public Pager()
        {
            m_PageSize = 50;
            PageNumber = 0;
        }

        public uint PageNumber
        {
            get
            {
                return m_CurrentPage;
            }
            set
            {
                uint from = m_PageSize * value;
                RecordFrom = from;
                RecordTo = from + m_PageSize  - 1;
                m_CurrentPage = value;
            }
        }

        public uint PageSize
        {
            get
            {
                return m_PageSize;
            }
            set
            {
                m_PageSize = value;
                PageNumber = 0;
            }
        }

        public uint RecordFrom
        {
            get
            {
                return m_RecordFrom;
            }
            private set
            {
                m_RecordFrom = value;
                RecordFromDisplay = value + 1;
            }
        }

        public uint RecordFromDisplay { get; private set; }

        public uint RecordTo
        {
            get
            {
                return m_RecordTo;
            }
            private set
            {
                m_RecordTo = value;
                RecordToDisplay = value + 1;
            }
        }

         public uint RecordToDisplay { get; private set; }

        public event EventHandler PagesChanged;

        public bool CanPreviousPage()
        {
            return PageNumber > 0;
        }

        public void NextPage()
        {
            PageNumber++;
            OnPagesChanged();
        }

        [Preview("CanPreviousPage")]
        [Dependencies("PageNumber")]
        public void PreviousPage()
        {
            if (CanPreviousPage())
            {
                PageNumber--;
                OnPagesChanged();
            }
        }

        private void OnPagesChanged()
        {
            if (PagesChanged != null)
            {
                PagesChanged(this, EventArgs.Empty);
            }
        }
    }

and the view:

<UserControl x:Class="GridToolbar"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ca="clr-namespace:Caliburn.Actions;assembly=Caliburn.Actions"
    xmlns:cm="clr-namespace:Caliburn.RoutedUIMessaging;assembly=Caliburn.RoutedUIMessaging"
    >
    <Action.Target>
        <Binding />
    </Action.Target>

    <UserControl.Resources>
        <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="MainWindowResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <DockPanel LastChildFill="True">
        <ToolBar DockPanel.Dock="Top">
            <Button ca:Action.Target="{Binding Pager}" cm:Message.Attach="PreviousPage">
                <Image Source="..\Resources\Images\arrow_180.png" Opacity="1" Stretch="None" VerticalAlignment="Center" />
            </Button>
            <Label Content="{Binding Pager.RecordFromDisplay}"></Label>
            <Label>...</Label>
            <Label Content="{Binding Pager.RecordToDisplay}"></Label>
            <Button ca:Action.Target="{Binding Pager}" cm:Message.Attach="NextPage">
                <Image Source="..\Resources\Images\arrow.png" Opacity="1" Stretch="None" VerticalAlignment="Center" />
            </Button>
            <Separator></Separator>
            <Button cm:Message.Attach="OnShowDetail">
                <Image Source="..\Resources\Images\document_horizontal_text.png" Opacity="1" Stretch="None" VerticalAlignment="Center" />
            </Button>
            <Button cm:Message.Attach="LoadData">
                <Image Source="..\Resources\Images\arrow_circle_double.png" Opacity="1" Stretch="None" VerticalAlignment="Center" />
            </Button>
            <Button cm:Message.Attach="OnGoToFilter">
                <Image Source="..\Resources\Images\binocular.png" Opacity="1" Stretch="None" VerticalAlignment="Center" />
            </Button>
        </ToolBar>
    </DockPanel>
</UserControl>

Please notice how caliburn simplified the code! Because the DataContext is set to a grid controller certain actions could be bound to it directly, like OnGoToFilter, but buttons for paging was to be bound to the Pager class, so additionally I had to indicate the action target. Clean and simple.

And how it looks like:

wpftoolbar

It is without any particular styling. The left one of course, the right one is from my winforms project. And don’t complain on blury text and images, MS will fix it soon (I hope so).

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.