大佬教程收集整理的这篇文章主要介绍了Lightswitch中树形控件的使用,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Lightswitch中没有树形展示的方式,也未找到第三方的扩展,只好用silverlight toolkit中的tree来实现。主要实现部门功能(自引用关系)
表定义如下
ID,deptname,ParentID
在LightSwitch中数据实体如下:
后来加了一个IsRoot 主要是用来获取根节点,Ls在查询条件时没办法设置参数值为null,只好将就一下来解决了。
建一个silverligth类库工程解决树的问题,主要二个文件,代码后续。
1.在需要树展示的屏幕中添加一个本地变量,类型Dept类型,同时编辑其过涉条件为IsRoot=true
2.在需要树展示的屏幕合适位置中添加一个自定义控件(就是我们建的工程,将数据源Screen赋上),命名为DeptTree(后面会用到)
3,在屏幕保存的地方,加上保存处理
partial void EditableDeptmentSetGrid_Saved()
{
// 在此编写您的代码。
DeptTree.refresh();
<UserControl x:Class="SilverLightclassLibrary1.SilverlightTreeControl" xmlns="http://scheR_30_11845@as.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://scheR_30_11845@as.microsoft.com/winfx/2006/xaml" xmlns:d="http://scheR_30_11845@as.microsoft.com/expression/blend/2008" xmlns:mc="http://scheR_30_11845@as.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SilverLightclassLibrary1" xmlns:sdk="http://scheR_30_11845@as.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.resources> <local:EntityCollectionValueConverter x:Key="EntityCollectionValueConverter" /> </UserControl.resources> <Grid x:Name="LayoutRoot" BACkground="White"> <StackPanel Orientation="Horizontal"> <sdk:TreeView Name="treeViewControl" Itemssource="{Binding Screen.DeptTreE}"> <sdk:TreeView.ItemContainerStyle> <Style TargetType="sdk:TreeViewItem"> <Setter Property="IsExpanded" Value="True"/> </Style> </sdk:TreeView.ItemContainerStyle> <sdk:TreeView.ItemTemplate> <sdk:HierarchicalDataTemplate Itemssource="{Binding Converter={Staticresource EntityCollectionValueConverter},ConverterParameter=SubDepts}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Path=deptname,Mode=TwoWay}" Margin="5,0" Width="74" /> </StackPanel> </sdk:HierarchicalDataTemplate> </sdk:TreeView.ItemTemplate> </sdk:TreeView> </StackPanel> </Grid> </UserControl>
// By: Karol Zadora-Przylecki (Microsoft) // From: http://bit.ly/oNTsJo // Ensures that an entity collection is loaded before accessed by UI thread. // The binding should use the whole entity object as data binding context public class EntityCollectionValueConverter : IValueConverter { public object Convert(object value,Type targetType,object parameter,System.Globalization.CultureInfo culturE) { String strErrormessage = "Converter parameter should be set to the property name that will serve as source of data"; IEntityObject entity = value as IEntityObject; if (entity == null) throw new Argumentexception("The converter should be using an entity object"); String sourcePropertyName = parameter as String; if (String.IsNullOrWhiteSpace(sourcePropertyName)) throw new Argumentexception(strErrormessagE); // Enumerate the source property using logic dispatcher // and prepare the collection of entities that the control will bind to var entities = new ObservableCollection<IEntityObject>(); var temporaryEntites = new List<IEntityObject>(); entity.Details.Dispatcher.beginInvoke(delegate { IEntityCollection eCollection = entity.Details.Properties[sourcePropertyName].Value as IEntityCollection; if (eCollection == null) { Debug.Assert(false,"The property " + sourcePropertyName + " is not an entity collection"); return; } // Now we are on the logic thread. We cAnnot just stuff the observable collection // with entities because the collection will immediately raise Changed events // and this will result in invalid cross-thread access. So we'll use a temporary collection // and copy the entites again on the UI thread foreach (IEntityObject e in eCollection) { temporaryEntites.Add(E); } Microsoft.LightSwitch.Threading.Dispatchers.Main.beginInvoke(delegate { // I wish ObservableCollection had an AddRange() method... foreach (IEntityObject e in temporaryEntites) { entities.Add(E); } }); }); return entities; } public object ConvertBACk(object value,System.Globalization.CultureInfo culturE) { throw new NotImplementedException(); } }
以上是大佬教程为你收集整理的Lightswitch中树形控件的使用全部内容,希望文章能够帮你解决Lightswitch中树形控件的使用所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。