稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager

稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager,第1张

概述[索引页] [源码下载] 稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager 作者:webabcd 介绍 Silverlight 3.0 控件一览: AutoCompleteBox - 自动完成控件。当用户输入部分信息后,此控件可以基于指定的过滤算法在一个下拉框中陈列出匹配项 DataPager - 分页控件  在线DEMO http:// [索引页]
[源码下载]


稳扎稳打Silverlight(33) - 3.0控件之autocompletebox,DataPager

作者:webabcd


介绍
Silverlight 3.0 控件一览:
autocompletebox - 自动完成控件。当用户输入部分信息后,此控件可以基于指定的过滤算法在一个下拉框中陈列出匹配项 DataPager - 分页控件 

在线DEMO
http://www.voidcn.com/article/p-boakcggz-tq.html  


示例
1、演示 autocompletebox(一次绑定全部数据或按需加载相关数据)
autocompletebox.xaml <navigation:Page xmlns:input="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.input"    x:Class="Silverlight30.Control.autocompletebox"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                     mc:Ignorable="d"
                     xmlns:navigation="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
                     d:DesignWIDth="640" d:DesignHeight="480"
                     title="autocompletebox Page">
        <GrID x:name="LayoutRoot">
                
                <GrID.Resources>
                        <!--用于在 autocompletebox 中显示数据的模板-->
                        <DataTemplate x:Key="dataTemplate">
                                <StackPanel OrIEntation="Horizontal">
                                        <TextBlock Text="名字: " />
                                        <TextBlock Text="{Binding name}" />
                                        <TextBlock Text="薪水: " />
                                        <TextBlock Text="{Binding Salary}" />
                                </StackPanel>
                        </DataTemplate>
                </GrID.Resources>

                <StackPanel OrIEntation="Horizontal" VerticalAlignment="top">
                        
                        <!--
                                MinimumPrefixLength - 如需显示自动完成的匹配项,所需输入的最少字符数
                                IsTextCompletionEnabled - 是否在 Text 中显示当前匹配项的全部内容
                                MaxDropDownHeight - 下拉框的最大长度
                                FilterMode - 根据用户的输入,对数据源做过滤的方式,默认值:StartsWith [System.windows.Controls.autoCompleteFilterMode 枚举]
                                        本例演示如何实现自定义的过滤
                                DropDownopening,DropDownopened,DropDownClosing,DropDownClosed - 顾名思义的几个事件
                        -->
                        <input:autocompletebox x:name="autocompletebox" WIDth="100" Height="30" margin="10"
                                                                     MinimumPrefixLength="0" IsTextCompletionEnabled="True" MaxDropDownHeight="100"
                                                                     FilterMode="Custom">
                                <!--
                                        呈现数据的方式如下,也可以设置 autocompletebox 的 ValueMemberBinding 属性
                                -->
                                <input:autocompletebox.ItemTemplate>
                                        <DataTemplate>
                                                <StackPanel>
                                                        <TextBlock Text="{Binding}" />
                                                </StackPanel>
                                        </DataTemplate>
                                </input:autocompletebox.ItemTemplate>
                        </input:autocompletebox>

                        
                        <!--
                                ValueMemberPath - 在此属性指定的成员中做过滤,过滤参数为用户的输入
                                ItemTemplate - 指定用于显示数据的模板
                        -->
                        <input:autocompletebox x:name="autocompleteboxTemplate" WIDth="100" Height="30" margin="10"
                                                                     ValueMemberPath="Salary"
                                                                     ItemTemplate="{StaticResource dataTemplate}" />

                        
                        <!--
                                Populating,Populated - 调用 按需加载数据服务 的一对事件
                                MinimumPopulateDelay - 调用 按需加载数据服务 的延迟时间。即在用户的输入发生改变时,此时间后调用指定的服务
                        -->
                        <input:autocompletebox x:name="autocompleteboxPopulate" WIDth="100" Height="30" margin="10"    
                                                                     Populating="autocompleteboxPopulate_Populating"
                                                                     MinimumPopulateDelay="500">
                                <input:autocompletebox.ItemTemplate>
                                        <DataTemplate>
                                                <StackPanel>
                                                        <TextBlock Text="{Binding}" />
                                                </StackPanel>
                                        </DataTemplate>
                                </input:autocompletebox.ItemTemplate>
                        </input:autocompletebox>
                        
                </StackPanel>
        </GrID>
</navigation:Page>   EmployeeModel.cs

using System;

using System.Net;

using System.windows;

using System.windows.Controls;

using System.windows.documents;

using System.windows.Ink;

using System.windows.input;

using System.windows.Media;

using System.windows.Media.Animation;

using System.windows.Shapes;


namespace Silverlight30.Model

{

         public class EmployeeModel

        {

                 public string name { get; set; }


                 public double Salary { get; set; }


                 public DateTime DateOfBirty { get; set; }

        }

}   autocompletebox.xaml.cs

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.windows.Navigation;


using Silverlight30.Model;

using System.Xml.linq;


namespace Silverlight30.Control

{

         public partial class autocompletebox : Page

        {

                 public autocompletebox()

                {

                        InitializeComponent();


                         this.Loaded += new RoutedEventHandler(autocompletebox_Loaded);

                }


                 voID autocompletebox_Loaded( object sender,RoutedEventArgs e)

                {

                        Init();

                        Init2();

                }


                 private voID Init()

                {

                         // IsDropDownopen - 是否显示自定完成的下拉框

                        autocompletebox.GotFocus += delegate { autocompletebox.IsDropDownopen = true; };

                        autocompletebox.Focus();



                        List< string> collection = new List< string>();

                        collection.Add( "aabb");

                        collection.Add( "aabc");

                        collection.Add( "abcc");

                        collection.Add( "abbc");

                        collection.Add( "aaab");

                        collection.Add( "bcca");

                        collection.Add( "bbac");

                        collection.Add( "cbaa");

                        collection.Add( "ccaa");

                        collection.Add( "cccb");

                        collection.Add( "cccc");

                        collection.Add( "cabc");

                        collection.Add( "cabb");


                        autocompletebox.ItemsSource = collection;



                         /*

                         * ItemFilter - 过滤下拉框内的对象

                         * TextFilter - 过滤下拉框内的字符串

                         * SearchText - 以此值为参数,过滤下拉框中的数据

                         * SelectedItem - 下拉框当前所选中的对象

                         */


                         // 自定义 FilterMode

                         // 第一个参数:用户输入的值;第二个参数:下拉框中的对象

                        autocompletebox.ItemFilter += (search,value) =>

                        {

                                 if (value.ToString().Tolower().StartsWith(search.Tolower()) || value.ToString().Tolower().EndsWith(search.Tolower()))

                                         return true;


                                 return false;

                        };

                }


                 private voID Init2()

                {

                        List<EmployeeModel> employees = new List<EmployeeModel>();

                        employees.Add( new EmployeeModel { name = "aabb",DateOfBirty = DateTime.Now,Salary = 111 });

                        employees.Add( new EmployeeModel { name = "aabc",Salary = 112 });

                        employees.Add( new EmployeeModel { name = "abcc",Salary = 113 });

                        employees.Add( new EmployeeModel { name = "abbc",Salary = 114 });

                        employees.Add( new EmployeeModel { name = "aaab",Salary = 115 });

                        employees.Add( new EmployeeModel { name = "bcca",Salary = 116 });

                        employees.Add( new EmployeeModel { name = "bbac",Salary = 117 });

                        employees.Add( new EmployeeModel { name = "cbaa",Salary = 118 });

                        employees.Add( new EmployeeModel { name = "ccaa",Salary = 119 });

                        employees.Add( new EmployeeModel { name = "cccb",Salary = 1111 });

                        employees.Add( new EmployeeModel { name = "cccc",Salary = 1112 });

                        employees.Add( new EmployeeModel { name = "cabc",Salary = 1113 });

                        employees.Add( new EmployeeModel { name = "cabb",Salary = 1114 });


                        autocompleteboxTemplate.ItemsSource = employees;

                }


                 /// <summary>

                 /// 演示如何实现按需加载下拉框的数据

                 /// </summary>

                 private voID autocompleteboxPopulate_Populating( object sender,PopulatingEventArgs e)

                {

                         // Populate 是异步的,调用服务也是异步的

                         // 所以要先在 Populating 中 Cancel 掉 Populate,以便异步调用服务

                         // 服务返回结果后再调用 PopulateComplete() 方法,以便触发 Populated 事件


                        e.Cancel = true;


                        List< string> names = new List< string>();

                        Uri uri = new Uri( "http://localhost:8616/Employee.svc/names/" + e.Parameter,UriKind.absolute);


                        WebClIEnt clIEnt = new WebClIEnt();

                        clIEnt.DownloadStringCompleted += (s,args) =>

                        {

                                if (args.Error != null)

                                {

                                        MessageBox.Show("调用服务出错" + args.Error.ToString());

                                        return;

                                }


                                Xdocument xml = Xdocument.Parse(args.Result);

                                Xnamespace ns = "http://schemas.microsoft.com/2003/10/Serialization/Arrays";

                                autocompleteboxPopulate.ItemsSource = xml.Root.Elements(ns + "string").Select(p => p.Value).ToList();

                                autocompleteboxPopulate.PopulateComplete();

                        };

                        clIEnt.DownloadStringAsync(uri);

                }

        }

}  

2、演示 DataPager
DataPager.xaml <navigation:Page xmlns:data="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data"    x:Class="Silverlight30.Control.DataPager"    
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
                     xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                     mc:Ignorable="d"
                     xmlns:navigation="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Navigation"
                     d:DesignWIDth="640" d:DesignHeight="480"
                     title="DataPager Page">
        <GrID x:name="LayoutRoot">
                <StackPanel>
                        
                        <!--
                                PageSize - 页大小
                                NumericbuttonCount - 数字分页按钮的数量
                                autoEllipsis - 当页总数大于分页按钮的数量时,是否自动显示省略号
                                IsTotalitemCountFixed - 数据量是否是不变的(即是否没有对当前绑定数据的添加/删除 *** 作)
                                displayMode - 分页控件的显示模式 [System.windows.Controls.Data.PagerdisplayMode 枚举]
                        -->
                        <StackPanel margin="10" >
                                <data:DataPager x:name="dataPager"
                                                        PageSize="6" NumericbuttonCount="10" autoEllipsis="True"
                                                        displayMode="FirstLastPrevIoUsNext"    
                                                        IsTotalitemCountFixed="True">
                                </data:DataPager>
                                <ListBox x:name="ListBox" />
                                <data:DataPager x:name="dataPager2"
                                                        PageSize="6" NumericbuttonCount="10" autoEllipsis="True"
                                                        displayMode="FirstLastPrevIoUsNextNumeric"    
                                                        IsTotalitemCountFixed="True">
                                </data:DataPager>
                        </StackPanel>

                </StackPanel>
        </GrID>
</navigation:Page>   DataPager.xaml.cs

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.windows.Navigation;


using System.windows.Data;

using System.Xml.linq;


namespace Silverlight30.Control

{

         public partial class DataPager : Page

        {

                 public DataPager()

                {

                        InitializeComponent();


                         this.Loaded += new RoutedEventHandler(DataPager_Loaded);

                }


                 voID DataPager_Loaded( object sender,RoutedEventArgs e)

                {

                        Init();

                }


                 voID Init()

                {

                        List< string> items = new List< string>();

                         for ( int i = 0; i < 100; i++)

                        {

                                items.Add(i.ToString().Padleft(10,'0'));

                        }


                         // PagedCollectionVIEw - 使一个 IEnumerable 集合具有分页功能

                        PagedCollectionVIEw vIEw = new PagedCollectionVIEw(items);


                         // 设置 DataPager 的 Source 属性 和 对应的显示数据的控件的 ItemsSource 属性 为同一个 PagedCollectionVIEw 对象

                        dataPager.source = vIEw;

                        dataPager2.source = vIEw;

                        ListBox.ItemsSource = vIEw;

                }

        }

}    
OK
[源码下载]
总结

以上是内存溢出为你收集整理的稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager全部内容,希望文章能够帮你解决稳扎稳打Silverlight(33) - 3.0控件之AutoCompleteBox, DataPager所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存