Lightswitch does not have a tree display method or third-party extensions, so we have to use the tree in Silverlight toolkit to implement it. Mainly implement the department function (self-reference relationship)
The table is defined as follows:
ID, deptname, parentid
In lightswitch, the data entity is as follows:
Later, I added an isroot to obtain the root node. ls cannot set the parameter value to null when querying the condition, so I have to solve it.
Build a silverlikes class library project to solve the tree problem, mainly two files, the code is later.
1. Add a local variable of the dept type to the screen to be displayed, and edit the parameter isroot = true.
2. Add a custom control (that is, assign the data source screen to the project we created) to the appropriate position on the screen where the tree is to be displayed, and name it depttree (which will be used later)
3. Add the Save processing to the place where the screen is saved.
Partial void editabledeptmentsetgrid_saved ()
{
// Write your code here.
Depttree. Refresh ();
}
<UserControl x:Class="SilverlightClassLibrary1.SilverlightTreeControl" 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" xmlns:local="clr-namespace:SilverlightClassLibrary1" xmlns:sdk="http://schemas.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, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }