代码片段
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"x:Class="LightSwitchApplication.TreeViewControl.DepartmentTreeViewControl"    xmlns:local="clr-namespace:LightSwitchApplication.TreeViewControl"    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"SelectedItemChanged="treeViewControl_SelectedItemChanged"ItemsSource="{Binding Screen.DepartmentTree}">                <sdk:TreeView.ItemTemplate>                    <sdk:HierarchicalDataTemplate                             ItemsSource="{Binding                         Converter={StaticResource EntityCollectionValueConverter},                         ConverterParameter=Children}">                        <StackPanel Orientation="Horizontal">                            <!--<CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay}"/>-->                            <TextBlock Text="{Binding Path=Name, Mode=TwoWay}"                                       Margin="5,0"Width="74"/>                        </StackPanel>                    </sdk:HierarchicalDataTemplate>                </sdk:TreeView.ItemTemplate>            </sdk:TreeView>        </StackPanel>    </Grid></UserControl> | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | usingMicrosoft.LightSwitch.Framework.Client;usingMicrosoft.LightSwitch.Presentation;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Animation;usingSystem.Windows.Shapes;namespaceLightSwitchApplication.TreeViewControl{    publicpartialclassDepartmentTreeViewControl : UserControl    {        publicDepartmentTreeViewControl()        {            InitializeComponent();        }        privatevoidtreeViewControl_SelectedItemChanged(objectsender, RoutedPropertyChangedEventArgs<object> e)        {            varselectItem = (LightSwitchApplication.Department)treeViewControl.SelectedItem;            vartype1 = selectItem.GetType();            varcontext = (IContentItem)this.DataContext;            varscreen = context.Screen;            vardata = (VisualCollection<LightSwitchApplication.Department>)screen.Details.Properties["DepartmentTree"].Value;            data.SelectedItem = selectItem;            //data.text= selectItem.Details.Properties["Id"].Value;        }    }} | 
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | usingMicrosoft.LightSwitch;usingSystem;usingSystem.Collections.Generic;usingSystem.Collections.ObjectModel;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Ink;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Animation;usingSystem.Windows.Shapes;namespaceLightSwitchApplication.TreeViewControl{    publicclassEntityCollectionValueConverter : IValueConverter    {        publicobjectConvert(objectvalue,            Type targetType,            objectparameter,            System.Globalization.CultureInfo culture)        {            stringstrErrorMessage                = "Converter parameter should be set to the property name that will serve as source of data";            IEntityObject entity = value asIEntityObject;            if(entity == null)                thrownewArgumentException("The converter should be using an entity object");            stringsourcePropertyName = parameter asstring;            if(string.IsNullOrWhiteSpace(sourcePropertyName))                thrownewArgumentException(strErrorMessage);            // Enumerate the source property using logic dispatcher             // and prepare the collection of entities that the control will bind to            varentities = newObservableCollection<IEntityObject>();            vartemporaryEntites = newList<IEntityObject>();            entity.Details.Dispatcher.BeginInvoke(delegate            {                IEntityCollection eCollection =                    entity.Details.Properties[sourcePropertyName].Value asIEntityCollection;                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 ineCollection)                {                    temporaryEntites.Add(e);                }                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(delegate                {                    // I wish ObservableCollection had an AddRange() method...                    foreach(IEntityObject e intemporaryEntites)                    {                        entities.Add(e);                    }                });            });            returnentities;        }        publicobjectConvertBack(objectvalue,            Type targetType, objectparameter, System.Globalization.CultureInfo culture)        {            thrownewNotImplementedException();        }    }} | 
lightswitch 添加 TreeView 控件,布布扣,bubuko.com
原文:http://www.cnblogs.com/neozhu/p/3692003.html