大佬教程收集整理的这篇文章主要介绍了Silverlight MVVM 贴近实战(四),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
有的大牛只写怎样怎样理解概念,喜欢讲一些概念,有的大牛干脆不写技术,有的大牛只会唯利是图,卖一些组件,我不是大牛,我喜欢帮助别人。
今天我们看看档案管理界面的数据查询与展示,先上一张图,调一下胃口。
这个界面主要的看点有DataGrid数据展示,分组,分页,Convert的使用等。首先我们来看一下界面的代码,这个布局方式采用Grid+StackPanel布局,代码如下。
- <controls:ChildWindow.resources>
- <StateConvert:ArchiveStateConvert x:Key="ArchiveConvert"></StateConvert:ArchiveStateConvert>
- <StateConvert:SexConvert x:Key="SexConvert"></StateConvert:SexConvert>
- <Localresource:ArchiveManageresource x:Key="Localresource"></Localresource:ArchiveManageresource>
- <Style x:Key="columnHeaderStyle" TargetType="sdk:DataGridcolumnHeader">
- <Setter Property="FontSize" Value="12"></Setter>
- </Style>
- </controls:ChildWindow.resources> 省略部分代码
- <TextBlock Text="{Binding tb_Name,source={Staticresource LocalresourcE}}" @H_854_49@margin="10,0" FontSize="12" Grid.Row="0" Grid.column="5"/>
- <sdk:AutoCompleteBox Width="130" FontSize="12" Grid.Row="0" Grid.column="6" Itemssource="{Binding PersonList,Mode=OneWay}" SELEctedItem="{Binding SELEctedPerson,Mode=TwoWay}"/>
- <StackPanel Grid.Row="3" Grid.column="2" Grid.columnSpan="3" @H_854_49@margin="0,5,0" Orientation="Horizontal" HorizontalAlignment="Center">
- <Button Content="{Binding btn_Search,source={Staticresource LocalresourcE}}" Command="{Binding QueryCommands}" FontSize="12" Width="90" @H_854_49@margin="0,0"/>
- <Button Content="{Binding btn_Reset,source={Staticresource LocalresourcE}}" FontSize="12" Width="90"/>
- </StackPanel>
- </Grid>
- </Border>
- <sdk:DataGrid Grid.Row="0" Grid.column="0" Grid.columnSpan="2"
- IsReadOnly="True" x:Name="dgArchiveList"
- Itemssource="{Binding MyCollectionViewsource.View,Mode=OneWay}"
- AutoGeneratecolumns="false"
- AlternaTingRowBACkground="Silver"
- columnHeaderHeight="20"
- FrozencolumnCount="3"
- CanUserReordercolumns="True"
- VerticalScrollBarVisibility="Auto"
- HorizontalScrollBarVisibility="Auto"
- AreRowGroupHeadersFrozen="false"
- SELEctionMode="Single"
- CanUserSortcolumns="false"
- LoadingRowGroup="MyDataGrid_LoadingRowGroup">
- <sdk:DataGrid.columns>
- <!--<sdk:DataGridTemplatecolumn Header="选择">
- <sdk:DataGridTemplatecolumn.CellTemplate>
- <DataTemplate>
- <checkBox checked="{Binding Ischecked,Mode=TwoWay}"/>
- </DataTemplate>
- </sdk:DataGridTemplatecolumn.CellTemplate>
- </sdk:DataGridTemplatecolumn>-->
- <sdk:DataGridTextcolumn Binding="{Binding archiveState,Mode=OneWay,Converter={Staticresource archiveConvert}}">
- <sdk:DataGridTextcolumn.HeaderStyle>
- <Style TargetType="sdk:DataGridcolumnHeader">
- <Setter Property="ContentTemplate">
- <Setter.Value>
- <DataTemplate>
- <TextBlock Text="{Binding Grid_Header_ArchiveState,source={Staticresource LocalresourcE}}"/>
- </DataTemplate>
- </Setter.Value>
- </Setter>
- <Setter Property="FontSize" Value="12"/>
- </Style>
- </sdk:DataGridTextcolumn.HeaderStyle>
- </sdk:DataGridTextcolumn>
- <sdk:DataGridTextcolumn HeaderStyle="{Staticresource columnHeaderStylE}" Header="档案编号" Binding="{Binding archiveNo,Mode=OneWay}" />
- 省略部分代码
- </sdk:DataGrid.columns>
- </sdk:DataGrid> 省略部分代码
- <StackPanel Grid.Row="1" Grid.column="1" Orientation="Horizontal" HorizontalAlignment="Right" @H_854_49@margin="0,10,0">
- <HyperlinkButton x:Name="linkFirst" Foreground="Blue" Tag="First" IsEnabled="{Binding IsLinkFirstEnable,Mode=OneWay}" Click="HyperlinkButton_Click" Content="{Binding link_First,source={Staticresource LocalresourcE}}" FontSize="12"/>
- <HyperlinkButton x:Name="linkPrevIoUs" Foreground="Blue" Tag="PrevIoUs" IsEnabled="{Binding IsLinkPrevIoUsEnable,Mode=OneWay}" Click="HyperlinkButton_Click" Content="{Binding link_PrevIoUs,source={Staticresource LocalresourcE}}" FontSize="12"/>
- <HyperlinkButton x:Name="linkNext" Foreground="Blue" Tag="Next" IsEnabled="{Binding IsLinkNextEnable,Mode=OneWay}" Click="HyperlinkButton_Click" Content="{Binding link_Next,source={Staticresource LocalresourcE}}" FontSize="12"/>
- <HyperlinkButton x:Name="linkLast" Foreground="Blue" Tag="Last" IsEnabled="{Binding IsLinkLastEnable,Mode=OneWay}" Click="HyperlinkButton_Click" Content="{Binding link_Last,source={Staticresource LocalresourcE}}" FontSize="12"/>
- </StackPanel>
- </controls:ChildWindow>
查询条件中使用了AutoComplete,<sdk:AutoCompleteBox Width="130" FontSize="12" Grid.Row="0" Grid.column="6" Itemssource="{Binding PersonList,Mode=OneWay}" SELEctedItem="{Binding SELEctedPerson,Mode=TwoWay}"/>。首先其Itemssource绑定的是一个List<String>,SELEctedItem绑定的是选择项,我们看看效果。
当我在姓名文本框中输入“李”的时侯,所有姓李的人都列出来了。当我选择了一个人名以后,SELEctedPerson的值将会变为我选择的人名,因为SELEctedPerson是个双向绑定。ok我们再往下看,我们发现前台代码的DataGrid列中有这么一行。
- <sdk:DataGridTextcolumn Binding="{Binding archiveState,Converter={Staticresource archiveConvert}}">
- <sdk:DataGridTextcolumn.HeaderStyle>
- <Style TargetType="sdk:DataGridcolumnHeader">
- <Setter Property="ContentTemplate">
- <Setter.Value>
- <DataTemplate>
- <TextBlock Text="{Binding Grid_Header_ArchiveState,source={Staticresource LocalresourcE}}"/>
- </DataTemplate>
- </Setter.Value>
- </Setter>
- <Setter Property="FontSize" Value="12"/>
- </Style>
- </sdk:DataGridTextcolumn.HeaderStyle>
- </sdk:DataGridTextcolumn>
这个写法是因为假如我们要给列头国际化,因为DataGrid的DataGridTextcolumn的Header是不可以绑定resource文件的(因为columnHeader是DependencyObject类型的,而不是DepenencyProperty),所以我们只能给它指定Style。这就是这个列和其他列不同的原因,其他列是直接设置Header的。OK,再看这么一段代码
<sdk:DataGridTextcolumn HeaderStyle="{Staticresource columnHeaderStylE}" Header="性别" Binding="{Binding Sex,Converter={Staticresource SexConvert}}"/>
我们发现Binding中有个Converter属性,这个是用来转换性别的,假如Sex的值是1,转换为男,假如是0,转换为女。首先需要在页面引用这个实现了IValueConverter 的类,如上代码xmlns:StateConvert="clr-namespace:Client.Common;assembly=Common"先引用命名空间,在引用类<StateConvert:SexConvert x:Key="SexConvert"></StateConvert:SexConvert>,我们看看这个SexConvert 类,如下
- namespace Client.Common
- {
- public class SEXConvert : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culturE)
- {
- String s = (String)value;
- switch (s)
- {
- case "1":
- s = "男";
- break;
- case "0":
- s = "女";
- break;
- }
- return s;
- }
- public object ConvertBACk(object value, CultureInfo culturE)
- {
- String strValue = value.ToString().Equals("男") ? "1" : "0";
- return strValue;
- }
- }
- }
实现Convert和ConvertBACk即可。接下来我们要看的是DataGrid的分页和分组,和上一篇文章一样,查询绑定的是Command,所以逻辑都在viewmodel中。我们看看代码
- public ICommand QueryCommands
- {
- get
- {
- QueryCommand queryCommand = new QueryCommand();
- queryCommand.Query += delegate()
- {
- this.CurrentPage = 1;
- var queryrequest = new Searchrequest()
- {
- archiveNo = this.ArchiveNo,
- GraduateStartDate = this.StartDate,
- GraduateEndDate = this.EndDate,
- NAME = this.SELEctedPerson,
- SEX = this.Sex,
- BirthDay = this.birthDay,
- IdCard = this.IdCard,
- Professional = this.Professional,
- GraduateSchool = this.GraduateSchool,
- Education = this.Education,
- archiveState = this.ArchiveState,
- PageIndex = -1,
- PageSize = this.PageSize
- };
- GetData(queryrequest);
- };
- return queryCommand;
- }
- }
- public void GetData(Searchrequest queryrequest)
- {
- String requestStr = SeriealizeHelper<Searchrequest>.Jsonserialize<Searchrequest>(queryrequest);
- archiveInfoservice.ArchiveInfoserviceClient client = new archiveInfoservice.ArchiveInfoserviceClient();
- List<ArchiveInfoEntity> list = new List<ArchiveInfoEntity>();
- client.GetPersonInfoListCompleted += delegate(object sender, archiveInfoservice.GetPersonInfoListCompletedEventArgs E)
- {
- archiveInfoEntity entity;
- e.Result.PersonInfoList.ForEach(r =>
- {
- entity = new archiveInfoEntity();
- entity.Ischecked = false;
- entity.ArchiveNo = r.no;
- entity.ArchiveState = r.state;
- entity.birthDay = r.birth;
- entity.Education = r.education_level;
- entity.GraduateSchool = r.graduate_school;
- entity.GraduateYear = r.graduate_year;
- entity.IdCardNo = r.id_card;
- entity.Ischecked = false;
- entity.Name = r.name;
- entity.Professional = r.professional;
- entity.Sex = r.sex;
- list.Add(entity);
- });
- this.RecordCount = e.Result.RecordCount;
- this.@R_320_10586@lPage = e.Result.RecordCount % PageSize == 0 ? e.Result.RecordCount / PageSize : e.Result.RecordCount / PageSize + 1;
- this.SetLinkBtnEnable(queryrequest);
- CollectionViewsource collectionViewsource = new CollectionViewsource();
- collectionViewsource.source = list;
- collectionViewsource.GroupDescriptions.Add(new PropertyGroupDescription("ArchiveState"));
- this.MyCollectionViewsource = collectionViewsource;
- };
- client.GetPersonInfoListAsync(requestStr);
- }
- private void SetLinkBtnEnable(Searchrequest request)
- {
- this.SetLinkEnable(true);
- if (this.@R_320_10586@lPage <= 1)
- {
- this.SetLinkEnable(false);
- }
- if (request.PageIndex == this.@R_320_10586@lPage-2)
- {
- this.IsLinkNextEnable = false;
- this.IsLinkLastEnable = false;
- }
- if (request.PageIndex == -1 && this.@R_320_10586@lPage > 1)
- {
- this.IsLinkFirstEnable = false;
- this.IsLinkPrevIoUsEnable = false;
- }
- }
- private void SetLinkEnable(bool isEnablE)
- {
- this.IsLinkFirstEnable = isEnable;
- this.IsLinkLastEnable = isEnable;
- this.IsLinkNextEnable = isEnable;
- this.IsLinkPrevIoUsEnable = isEnable;
- }
从前台代码我们可以看出,分页按钮的isEnabled状态分别绑定viewmodel中的IsLinkFirstEnable ,IsLinkLastEnable,IsLinkNextEnable,IsLinkPrevIoUsEnable。在GetData中有这么一段代码,就是获取到数据源以后,我们对其进行了分组,
- this.RecordCount = e.Result.RecordCount;
- this.@R_320_10586@lPage = e.Result.RecordCount % PageSize == 0 ? e.Result.RecordCount / PageSize : e.Result.RecordCount / PageSize + 1;
- this.SetLinkBtnEnable(queryrequest);
- CollectionViewsource collectionViewsource = new CollectionViewsource();
- collectionViewsource.source = list;
- collectionViewsource.GroupDescriptions.Add(new PropertyGroupDescription("ArchiveState"));
- this.MyCollectionViewsource = collectionViewsource;
我们获取到分页数据以后,根据ArchiveState属性进行分组,然后绑定到界面,注意,DataGrid界面绑定代码Itemssource="{Binding MyCollectionViewsource.View,Mode=OneWay}",注意在这里是MyCollectionViewsource.View。接下来是分页,分页是我自己定义的一个分页。我们看看页面cs代码
- private void HyperlinkButton_Click(object sender, routedEventArgs E)
- {
- HyperlinkButton hyperLink = sender as HyperlinkButton;
- var queryrequest = new Searchrequest()
- {
- archiveNo = archiveModel.ArchiveNo,
- GraduateStartDate = archiveModel.StartDate,
- GraduateEndDate = archiveModel.EndDate,
- NAME = archiveModel.SELEctedPerson,
- SEX = archiveModel.Sex,
- BirthDay = archiveModel.birthDay,
- IdCard = archiveModel.IdCard,
- Professional = archiveModel.Professional,
- GraduateSchool = archiveModel.GraduateSchool,
- Education = archiveModel.Education,
- archiveState = archiveModel.ArchiveState,
- PageSize = archiveModel.PageSize,
- };
- switch (hyperLink.Tag.ToString())
- {
- case "First":
- archiveModel.PageIndex = -1;
- break;
- case "Next":
- archiveModel.PageIndex++;
- break;
- case "PrevIoUs":
- archiveModel.PageIndex--;
- break;
- case "Last":
- archiveModel.PageIndex = archiveModel.@R_320_10586@lPage-2;
- break;
- }
- archiveModel.CurrentPage = archiveModel.PageIndex + 2;
- queryrequest.PageIndex = archiveModel.PageIndex;
- archiveModel.GetData(queryrequest);
- }
在分页的时候调用viewmodel的GetData方法,在viewmodel中动态的设置分页按钮的enabled状态。为了方便大家理解我的意思,我会把viewmodel的cs作为附件供大家下载。ok,我们看看服务端分页代码,如下
- public SearchResponse GetPersonInfoList(String searchrequest)
- {
- Searchrequest searchrequests = serializeHelper.JsonDeserialize<Searchrequest>(searchrequest);
- IQueryable<Person_Info> personInfo = misInfoEntities.person_info
- .Where(p => (String.IsNullOrEmpty(searchrequests.ArchiveNo) ? true : searchrequests.ArchiveNo.Contains(p.no))
- && (String.IsNullOrEmpty(searchrequests.Name) ? true : p.name == searchrequests.Name)
- && (String.IsNullOrEmpty(searchrequests.SeX) ? true : p.sex == searchrequests.SeX)
- && (String.IsNullOrEmpty(searchrequests.IdCard) ? true : p.id_card.Contains(searchrequests.IdCard))
- && (String.IsNullOrEmpty(searchrequests.Professional) ? true : p.professional == searchrequests.Professional)
- && (String.IsNullOrEmpty(searchrequests.Education) ? true : p.education_level == searchrequests.Education)
- && (String.IsNullOrEmpty(searchrequests.ArchiveStatE) ? true : p.state == searchrequests.ArchiveStatE)
- && (!searchrequests.GraduateStartDate.HasValue ? true : p.graduate_year >= searchrequests.GraduateStartDate.Value.Year)
- && (!searchrequests.GraduateEndDate.HasValue ? true : p.graduate_year <= searchrequests.GraduateEndDate.Value.Year)
- && (!searchrequests.birthDay.HasValue ? true : p.birth == searchrequests.birthday));
- IEnumerable<Person_Info> personInfos = personInfo.AsEnumerable().orderBy(p => p.Name)
- .Skip(searchrequests.PageSize * (searchrequests.PageIndex + 1))
- .Take(searchrequests.PageSizE);
- foreach (var person in personInfos)
- {
- person.education_level = misInfoEntities.codes.SingLeorDefault(c => c.ename == "EDUCATION" && c.data == person.education_level).display_content;
- }
- return new SearchResponse() { PersonInfoList = personInfos.ToList(), recordCount = personInfo.Count() };
- }
在这里因为我的服务端的数据访问层是EF,所以采用的是LINQ to Entities的方式。今天就写到这里,因为篇幅有限,有很多代码没有贴,需要代码的同志可以留言,我可以发给大家,或者加入.net群205217091,我可以共享给大家。
以上是大佬教程为你收集整理的Silverlight MVVM 贴近实战(四)全部内容,希望文章能够帮你解决Silverlight MVVM 贴近实战(四)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。