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

Silverlight 自定义控件 模板化控件 (二)事件,第1张

概述源码下载:http://download.csdn.net/source/3387394 silverlight 自定义控件 事件,这里我制作一个自己的DataGrid,起内部包含了2个SLToolkit.DataPager和一个System.Windows.Controls.DataGrid。此控件主要实现2个功能。PageCount属性实现2个SLToolkit.DataPager自动按照要求

源码下载: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 Property@R_502_6135@data(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 Property@R_502_6135@data(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 Property@R_502_6135@data(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

总结

以上是内存溢出为你收集整理的Silverlight 自定义控件 模板化控件 (二)事件全部内容,希望文章能够帮你解决Silverlight 自定义控件 模板化控件 (二)事件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1077625.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存