lightswitch 添加 TreeView 控件

代码片段

<UserControl xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  x:Class="LightSwitchApplication.TreeViewControl.DepartmentTreeViewControl"
    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:local="clr-namespace:LightSwitchApplication.TreeViewControl"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    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>
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
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;

namespace LightSwitchApplication.TreeViewControl
{
    public partial class DepartmentTreeViewControl : UserControl
    {
        public DepartmentTreeViewControl()
        {
            InitializeComponent();
        }

        private void treeViewControl_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var selectItem = (LightSwitchApplication.Department)treeViewControl.SelectedItem;
            var type1 = selectItem.GetType();
            var context = (IContentItem)this.DataContext;
            var screen = context.Screen;
            var data = (VisualCollection<LightSwitchApplication.Department>)screen.Details.Properties["DepartmentTree"].Value;
            data.SelectedItem = selectItem;
            //data.text= selectItem.Details.Properties["Id"].Value;

        }

    }
}
using Microsoft.LightSwitch;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
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 LightSwitchApplication.TreeViewControl
{
    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();
        }
    }
}

 片段2

 public partial class CategoriesListDetail
    {
        private TreeView treeView = null;
        partial void CategoriesListDetail_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.

        }

        partial void CategoriesListDetail_Created()
        {
            // Write your code here.
            this.P_Name = "";
            this.RootNode.Load();
            this.FindControl("TreeViewControl").ControlAvailable += ((o, e) =>
            {
                treeView = e.Control as TreeView;
                treeView.BorderThickness = new Thickness(1);
                if (treeView.Items.Count == 0)
                {
                    foreach (var item in this.RootNode)
                    {
                        TreeViewItem rootItem = new TreeViewItem() { Header = item.Name, Tag = item.Id };
                        treeView.Items.Add(rootItem);
                    }
                    treeView.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(TreeViewItem_SelectedItemChanged);
                }
            });
        }
        private void TreeViewItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var parentItem = e.NewValue as TreeViewItem;
            this.P_Name = (string)parentItem.Header;
            this.p_id = (int)parentItem.Tag;
            ///when collection is refreshed the event SelectedNodeEmployees_Changed is hooked up
            ///do not use Load method to avoid caching
            this.SelectedChildrenNodes.Refresh();
        }

        

        partial void SelectedChildrenNodes_Changed(NotifyCollectionChangedEventArgs e)
        {
            if (treeView != null)
            {
                Dispatchers.Main.BeginInvoke(() =>
                {
                    var parentItem = treeView.SelectedItem as TreeViewItem;
                    if (parentItem != null)
                    {
                        if (parentItem.Items.Count == 0 && this.SelectedChildrenNodes.Count() > 0)
                        {
                            foreach (var item in this.SelectedChildrenNodes) //.Where(act => act.Title != "???")
                            {
                                TreeViewItem newChildItem = new TreeViewItem() { Header = item.Name, Tag = item.Id };
                                parentItem.Items.Add(newChildItem);
                            }
                        }
                    }
                });
            }
        }

    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏hbbliyong

.Net下SQLite的DBHelp

怎样获取SqLite请参考初识SqlLite ---.net连接数据库,怎样在SQLite使用Linq请参考在C#中利用Nuget包使用SQLite数据库和Li...

3014
来自专栏hbbliyong

LINQ分页和排序,skip和Take 用法

LINQ分页和排序,skip和Take 用法 dbconn.BidRecord.OrderBy(p=>p.bid_id).ToList<BidRecord>()...

3769
来自专栏跟着阿笨一起玩NET

linq to sql 三层架构中使用CRUD操作

1331
来自专栏跟着阿笨一起玩NET

C# SQL多条件查询拼接技巧

本文转载:http://blog.csdn.net/limlimlim/article/details/8638080

2861
来自专栏张善友的专栏

Using sqlite with .NET

The other day I found that there is a .NET wrapper for sqlite. sqlite is a very ...

2728
来自专栏跟着阿笨一起玩NET

获取MDI窗体的实例

           //检查指定MDI窗体的实例是否存在。             IsExistInstance(this, typeof(WindowsF...

540
来自专栏菩提树下的杨过

几种常见复合sql查询语句的linq写法[继续补充中]

1.IN 语句 尝试了很多次,好象linq死活不生成in语句,无奈只能做了下变换 例,要找出"手机大类"以及"手机下属所有子类"的产品 (手机大类的ID为D0...

2326
来自专栏菩提树下的杨过

温故而知新:类索引器

类索引器  1 using System;  2 using System.Collections.Generic;  3  4 namespace Pro...

20510
来自专栏木宛城主

曾经的代码系列——AJAX和JSON生成下拉列表框

代码测试使用 前台页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GetGradeC...

1877
来自专栏王磊的博客

HttpWebRequest采集读取网站挂载Cookie的通用方法

Asp.net 版本 HttpWebRequest采集时添加:httpWebRequest.CookieContainer = new CookieContai...

2915

扫码关注云+社区