private void Initialize(EntityViewerViewModel <TEntity> viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }
            ViewModel = viewModel;
            viewModel.PropertyChanged += viewModel_PropertyChanged;
            viewModel.PageSize         = 30;

            if (ViewModel.Items.Count == 0 && ViewModel.ParentModels == null)
            {
                viewModel.ParentModels = EntityParentSelectorViewModel.CreateModel <TEntity>(BussinessApplication.Current.ContextBuilder);
            }

            Grid grid = new Grid();

            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(100, GridUnitType.Star)
            });
            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(100, GridUnitType.Pixel)
            });
            if (viewModel.ParentModels != null && viewModel.ParentModels.Length != 0)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(200, GridUnitType.Pixel)
                });
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });

                ParentTree = new TreeView();
                ParentTree.BorderThickness = new Thickness();
                TreeViewItem item = new TreeViewItem();
                item.Header      = "全部";
                item.IsExpanded  = true;
                item.ItemsSource = GetParentItems(viewModel.ParentModels);
                ParentTree.Items.Add(item);
                ParentTree.SelectedItemChanged += ParentTree_SelectedItemChanged;
                item.Margin = new Thickness(0, 0, 8, 0);
                grid.Children.Add(ParentTree);

                GridSplitter splitter = new GridSplitter();
                splitter.Width = 8;
                splitter.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                grid.Children.Add(splitter);
            }


            #region 列表显示

            ListView dataGrid = new ListView();
            dataGrid.MouseDoubleClick += dataGrid_MouseDoubleClick;
            dataGrid.BorderThickness   = new Thickness();
            if (viewModel.ParentModels != null && viewModel.ParentModels.Length != 0)
            {
                Grid.SetColumn(dataGrid, 1);
            }
            dataGrid.SelectionMode = SelectionMode.Extended;
            GridView view = new GridView();
            view.AllowsColumnReorder = false;
            var entityType        = typeof(TEntity);
            var viewBuilder       = viewModel.ViewBuilder;
            var visableProperties = viewBuilder.VisableProperties;
            var hideProperties    = viewBuilder.HideProperties;

            PropertyInfo[] properties;
            if (hideProperties == null)
            {
                properties = visableProperties.Select(v => entityType.GetProperty(v)).Where(t => t != null).ToArray();
            }
            else
            {
                properties = visableProperties.Except(hideProperties).Select(v => entityType.GetProperty(v)).Where(t => t != null).ToArray();
            }

            foreach (var property in properties)
            {
                GridViewColumn column  = new GridViewColumn();
                var            display = property.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
                if (display == null)
                {
                    column.Header = property.Name;
                }
                else
                {
                    column.Header = new Label {
                        Content = display.Name, ToolTip = display.Description
                    }
                };

                DataTemplate            dt  = new DataTemplate();
                FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Label));
                Binding binding             = new Binding();
                binding.Path = new PropertyPath(property.Name);
                fef.SetBinding(Label.ContentProperty, binding);
                dt.VisualTree       = fef;
                column.CellTemplate = dt;
                view.Columns.Add(column);
            }
            dataGrid.View = view;

            viewModel.UpdateSource();
            dataGrid.DataContext = viewModel;
            dataGrid.SetBinding(DataGrid.ItemsSourceProperty, new Binding("ItemsSource"));

            grid.Children.Add(dataGrid);

            #endregion

            #region  钮显示

            AppButtonPanel = new AppButtonPanel();
            Grid.SetRow(AppButtonPanel, 1);
            if (viewModel.ParentModels != null && viewModel.ParentModels.Length != 0)
            {
                Grid.SetColumnSpan(AppButtonPanel, 2);
            }
            grid.Children.Add(AppButtonPanel);

            AppButton back = new AppButton(new CustomCommand(null, Back));
            back.Text  = "返回";
            back.Image = (Canvas)Resources["appbar_arrow_left"];
            AppButtonPanel.Items.Add(back);

            AppButton previousPage = new AppButton(new CustomCommand(CanPreviousPage, PreviousPage));
            previousPage.Text  = "上一页";
            previousPage.Image = (Canvas)Resources["appbar_navigate_previous"];
            AppButtonPanel.Items.Add(previousPage);

            AppButton nextPage = new AppButton(new CustomCommand(CanNextPage, NextPage));
            nextPage.Text  = "下一页";
            nextPage.Image = (Canvas)Resources["appbar_navigate_next"];
            AppButtonPanel.Items.Add(nextPage);


            AppButton ok = new AppItemButton(dataGrid, OK);
            ok.Text  = "选择";
            ok.Image = (Canvas)Resources["appbar_check"];
            AppButtonPanel.Items.Add(ok);

            #endregion

            Content = grid;

            Loaded += Viewer_Loaded;
        }
 public MultipleSelector(EntityViewerViewModel <TEntity> viewModel)
 {
     Initialize(viewModel);
 }
Esempio n. 3
0
        private void Initialize(EntityViewerViewModel <TEntity> viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }

            Buttons = new System.Collections.ObjectModel.ObservableCollection <AppButton>();
            Buttons.CollectionChanged += Buttons_CollectionChanged;

            //Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml") });
            //Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
            //Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
            //Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml") });
            //Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml") });
            //Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/Wodsoft.ComBoost.Business.Remote;component/Resources/Icons.xaml") });

            ViewModel = viewModel;
            viewModel.PropertyChanged += viewModel_PropertyChanged;
            viewModel.PageSize         = 30;

            if (viewModel.AutoGenerateData && ViewModel.Items.Count == 0 && ViewModel.ParentModels == null)
            {
                viewModel.ParentModels = EntityParentSelectorViewModel.CreateModel <TEntity>(BussinessApplication.Current.ContextBuilder);
            }

            Grid grid = new Grid();

            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(100, GridUnitType.Star)
            });
            grid.RowDefinitions.Add(new RowDefinition {
                Height = new GridLength(100, GridUnitType.Pixel)
            });
            if (viewModel.ParentModels != null && viewModel.ParentModels.Length != 0)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(200, GridUnitType.Pixel)
                });
                grid.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(1, GridUnitType.Star)
                });

                ParentTree = new TreeView();
                ParentTree.BorderThickness = new Thickness();
                TreeViewItem item = new TreeViewItem();
                item.Header      = "全部";
                item.IsExpanded  = true;
                item.ItemsSource = GetParentItems(viewModel.ParentModels);
                ParentTree.Items.Add(item);
                ParentTree.SelectedItemChanged += ParentTree_SelectedItemChanged;
                item.Margin = new Thickness(0, 0, 8, 0);
                grid.Children.Add(ParentTree);

                GridSplitter splitter = new GridSplitter();
                splitter.Width = 8;
                splitter.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
                grid.Children.Add(splitter);
            }


            #region 列表显示
            View = new ListView();
            View.BorderThickness = new Thickness();
            if (viewModel.ParentModels != null && viewModel.ParentModels.Length != 0)
            {
                Grid.SetColumn(View, 1);
            }
            View.SelectionMode = SelectionMode.Single;
            GridView view = new GridView();
            view.AllowsColumnReorder = false;
            var entityType        = typeof(TEntity);
            var viewBuilder       = viewModel.ViewBuilder;
            var visableProperties = viewBuilder.VisableProperties;
            var hideProperties    = viewBuilder.HideProperties;

            PropertyInfo[] properties;
            if (hideProperties == null)
            {
                properties = visableProperties.Select(v => entityType.GetProperty(v)).Where(t => t != null).ToArray();
            }
            else
            {
                properties = visableProperties.Except(hideProperties).Select(v => entityType.GetProperty(v)).Where(t => t != null).ToArray();
            }

            foreach (var property in properties)
            {
                GridViewColumn column  = new GridViewColumn();
                var            display = property.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
                if (display == null)
                {
                    column.Header = property.Name;
                }
                else
                {
                    column.Header = new Label {
                        Content = display.Name, ToolTip = display.Description
                    }
                };

                DataTemplate            dt  = new DataTemplate();
                FrameworkElementFactory fef = new FrameworkElementFactory(typeof(Label));
                Binding binding             = new Binding();
                binding.Path = new PropertyPath(property.Name);
                fef.SetBinding(Label.ContentProperty, binding);
                dt.VisualTree       = fef;
                column.CellTemplate = dt;
                view.Columns.Add(column);
            }
            View.View = view;

            viewModel.UpdateSource();
            View.DataContext = viewModel;
            View.SetBinding(DataGrid.ItemsSourceProperty, new Binding("ItemsSource"));

            grid.Children.Add(View);

            #endregion

            #region  钮显示

            AppButtonPanel = new AppButtonPanel();
            Grid.SetRow(AppButtonPanel, 1);
            if (viewModel.ParentModels != null && viewModel.ParentModels.Length != 0)
            {
                Grid.SetColumnSpan(AppButtonPanel, 2);
            }
            grid.Children.Add(AppButtonPanel);

            AppButton back = new AppButton(new CustomCommand(null, Back));
            back.Text  = "返回";
            back.Image = (Canvas)Resources["appbar_arrow_left"];
            AppButtonPanel.Items.Add(back);

            //AppButton firstPage = new AppButton(new CustomCommand(CanFirstPage, FirstPage));
            //firstPage.Text = "第一页";
            //firstPage.Image = (Canvas)Application.Current.Resources["appbar_navigate_previous"];
            //AppButtonPanel.Items.Add(firstPage);

            AppButton previousPage = new AppButton(new CustomCommand(CanPreviousPage, PreviousPage));
            previousPage.Text  = "上一页";
            previousPage.Image = (Canvas)Resources["appbar_navigate_previous"];
            AppButtonPanel.Items.Add(previousPage);

            AppButton nextPage = new AppButton(new CustomCommand(CanNextPage, NextPage));
            nextPage.Text  = "下一页";
            nextPage.Image = (Canvas)Resources["appbar_navigate_next"];
            AppButtonPanel.Items.Add(nextPage);

            if (viewModel.ViewBuilder.AllowedAdd)
            {
                AppButton add = new AppButton(new CustomCommand(CanAdd, Add));
                add.Text  = "新增";
                add.Image = (Canvas)Resources["appbar_add"];
                AppButtonPanel.Items.Add(add);
            }

            if (viewModel.ViewBuilder.AllowedRemove)
            {
                AppButton remove = new AppItemButton(View, CanRemove, Remove);
                remove.Text  = "删除";
                remove.Image = (Canvas)Resources["appbar_delete"];
                AppButtonPanel.Items.Add(remove);
            }

            if (viewModel.ViewBuilder.AllowedEdit)
            {
                AppButton edit = new AppItemButton(View, CanEdit, Edit);
                edit.Text  = "编辑";
                edit.Image = (Canvas)Resources["appbar_edit"];
                AppButtonPanel.Items.Add(edit);
            }

            #endregion

            Content = grid;

            Loaded += Viewer_Loaded;
        }
        public MultipleSelector()
        {
            EntityViewerViewModel <TEntity> viewModel = new EntityViewerViewModel <TEntity>(BussinessApplication.Current.ContextBuilder.GetContext <TEntity>());

            Initialize(viewModel);
        }
Esempio n. 5
0
 public Viewer(EntityViewerViewModel <TEntity> viewModel)
 {
     Initialize(viewModel);
 }