微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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",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",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),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

@H_328_2502@

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐