Silverlight 自定义控件 模板化控件 (二)事件

源码下载:http://download.csdn.net/source/3387394

silverlight 自定义控件 事件,这里我制作一个自己的DataGrid,起内部包含了2个SLToolkit.DataPager和一个System.Windows.Controls.DataGrid。此控件主要实现2个功能。PageCount属性实现2个SLToolkit.DataPager自动按照要求分号页码。 PageIndexChanged事件视为PageIndex属性发生改变后公开的一个事件,有了它我们就可以知道页码索引变了。就可以实现简单的分页事件了。

    属性和事件我都是公开一个其它的功能或者事件大家自己可以扩展的去加。

如图

在Silverlight 自定义控件 模板化控件 (一)属性 里面我们完成了如何公开一个属性。

这里说说事件。

silverlight 自定义控件(模板化控件)的事件。

其实我们天天用微软工具栏的控件就是自定义控件,他们大多数控件都有很多的属性和事件。这里我们就来看看silverlight的自定义控件的事件是如何实现的呢。

      这里我们要借助上次被封装过的DataPager,它的PageCount属性。

我们接着在SLToolkit类库项目中建立一个silverlight模板化控件。起名为DataGrid。我们点确定后会看到系统会自动增加两个文件一个是DataGrid.cs一个是Themes文件夹下的Generic.xaml。

 

      简单介绍下这个新生成的文件Generic.xaml其实就是存放自定义控件模板的地方。

<ResourceDictionary xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="clr-namespace:SLToolkit">

 

 

    <Style TargetType="local:DataGrid">

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="local:DataGrid">

                    <Border Background="{TemplateBinding Background}"

                            BorderBrush="{TemplateBinding BorderBrush}"

                            BorderThickness="{TemplateBinding BorderThickness}">

                        <StackPanel >

                            <local:DataPager x:Name="DataPagerHead"></local:DataPager>

                            <sdk:DataGrid x:Name="DataGrid"></sdk:DataGrid>

                            <local:DataPager x:Name="DataPagerFood"></local:DataPager>                 

                        </StackPanel>

                    </Border>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

</ResourceDictionary>

 

 

 

<Style TargetType="local:DataGrid">标记为控件的模板内容

我们在模板中加入

<StackPanel >

                            <local:DataPager x:Name="DataPagerHead"></local:DataPager>

                            <sdk:DataGrid x:Name="DataGrid"></sdk:DataGrid>

                            <local:DataPager x:Name="DataPagerFood"></local:DataPager>                 

                        </StackPanel>

后台代码如下

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Input;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Shapes;

using System.Collections;

 

namespace SLToolkit

{

    [TemplatePart(Name = DataGrid.ElementFood, Type = typeof(DataPager))]

    [TemplatePart(Name = DataGrid.ElementHead, Type = typeof(DataPager))]

    [TemplatePart(Name = DataGrid.ElementDataGrid, Type = typeof(DataGrid))]

    public class DataGrid : Control

    {

        #region

        private const string ElementHead = "DataPagerHead";

        private const string ElementFood = "DataPagerFood";

        private const string ElementDataGrid = "DataGrid";

        #endregion

 

        #region

        internal DataPager _DataPagerHead;

        internal DataPager _DataPagerFood;

        internal System.Windows.Controls.DataGrid _DataGrid;

        #endregion

        /// <summary>

        /// DataPager¡Â°y?À?º?t

        /// </summary>

        public event EventHandler<EventArgs> PageIndexChanged;

        protected virtual void OnValueChanged(EventArgs e)

        {

            EventHandler<EventArgs> handler = PageIndexChanged;

 

            if (handler != null)

            {

                handler(this, e);

            }

        }

        /// <summary>

        /// ¡§°?Á¨¹°3ºy

        /// </summary>

        public int PageCount

        {

            get { return (int)this.GetValue(PagerPageCountProperty); }

            set

            {

                this.SetValue(PagerPageCountProperty, value);

            }

        }

 

        public static readonly DependencyProperty PagerPageCountProperty = DependencyProperty.Register("PageCount",

            typeof(int), typeof(DataGrid), new PropertyMetadata(0, new PropertyChangedCallback(PageCountChangedCallback)));

        private static void PageCountChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)

        {

 

            DataGrid dataGrid = (DataGrid)obj;

            //int _PageCount = (int)args.NewValue;

            if (dataGrid != null)

            {

                if (dataGrid._DataPagerHead != null && dataGrid._DataPagerFood != null)

                {

                    dataGrid.SetPageCount();

                }

            }

 

        }

        private void SetPageCount()

        {

            if (_DataPagerHead != null && _DataPagerFood != null)

            {

                _DataPagerHead.PageCount = PageCount;

                _DataPagerFood.PageCount = PageCount;

            }

        }

 

        #region ?°3¨¤¦¨´¬?

        /// <summary>

        /// ¡§°??°3¨¤¦¨´¬?

        /// </summary>

        public int PageSize

        {

            get

            {

                return (int)this.GetValue(PagerPageSizeProperty);

            }

            set

            {

                this.SetValue(PagerPageSizeProperty, value);

            }

        }

        public static readonly DependencyProperty PagerPageSizeProperty = DependencyProperty.Register("PageSize",

            typeof(int), typeof(DataGrid), new PropertyMetadata(0, new PropertyChangedCallback(PageSizeChangedCallback)));

        private static void PageSizeChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)

        {

 

            DataGrid dataGrid = (DataGrid)obj;

            //int _PageSize = (int)args.NewValue;

            dataGrid.SetPageSize();

 

 

        }

        private void SetPageSize()

        {

            if (_DataPagerHead != null && _DataPagerFood != null)

            {

                _DataPagerHead.PageSize = PageSize;

                _DataPagerFood.PageSize = PageSize;

            }

        }

 

        #endregion

 

 

        /// <summary>

        /// 索引页

        /// </summary>

        public int PageIndex

        {

            get

            {

                return (int)this.GetValue(PageIndexProperty);

            }

            set

            {

 

                this.SetValue(PageIndexProperty, value);

 

            }

        }

        public static readonly DependencyProperty PageIndexProperty = DependencyProperty.Register("PageIndex",

            typeof(int), typeof(DataGrid), new PropertyMetadata(new PropertyChangedCallback(PageIndexChangedCallback)));

        static void PageIndexChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)

        {

 

            DataGrid dataGrid = (DataGrid)obj;

            int _PageIndex = (int)args.NewValue;

            if (dataGrid != null)

            {  

                dataGrid.OnValueChanged(new EventArgs());            

            }

 

        }

        public IEnumerable ItemsSource

        {

            get

            {

                return this.GetValue(ItemsSourceProperty) as IEnumerable;

            }

            set

            {

                this.SetValue(ItemsSourceProperty, value);

            }

        }

        public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register("ItemsSource",

            typeof(IEnumerable), typeof(DataGrid), new PropertyMetadata(ItemsSourceChangedCallback));

        static void ItemsSourceChangedCallback(DependencyObject obj, DependencyPropertyChangedEventArgs args)

        {

            DataGrid dataGrid = (DataGrid)obj;

            IEnumerable _ItemsSource = (IEnumerable)args.NewValue;

            if (dataGrid._DataGrid != null)

            {

                dataGrid._DataGrid.ItemsSource = _ItemsSource;

            }

        }

        /// <summary>

        /// 1¨¬¡¥ºy

        /// </summary>

        public DataGrid()

        {

            this.DefaultStyleKey = typeof(DataGrid);

        }

        /// <summary>

        /// ?¡äOnApplyTemplate

        /// </summary>

        public override void OnApplyTemplate()

        {

            base.OnApplyTemplate();

            _DataPagerHead = this.GetTemplateChild(ElementHead) as DataPager;

            _DataPagerFood = this.GetTemplateChild(ElementFood) as DataPager;

            _DataGrid = this.GetTemplateChild(ElementDataGrid) as System.Windows.Controls.DataGrid;

            _DataPagerHead.PageIndexChanged += new EventHandler<EventArgs>(_DataPager_PageIndexChanged);

            _DataPagerFood.PageIndexChanged += new EventHandler<EventArgs>(_DataPager_PageIndexChanged);

            SetPageSize();

            SetPageCount();

        }

 

        private  void _DataPager_PageIndexChanged(object sender, EventArgs e)

        {

            DataPager dataPager = sender as DataPager;

            if (_DataPagerHead.Source != null && _DataPagerFood.Source != null)

            {

                if (dataPager.Equals(_DataPagerHead))

                {

                    _DataPagerFood.PageIndex = _DataPagerHead.PageIndex;

                }

                else if (dataPager.Equals(_DataPagerFood))

                {

                    _DataPagerHead.PageIndex = _DataPagerFood.PageIndex;

                }

            }

            PageIndex = _DataPagerFood.PageIndex;

        }

 

    }

}

 

 

 

 

 后续增加源码和 详细解释

源码下载:http://download.csdn.net/source/3387394

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值