Пример #1
0
 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
 {
     return(VisualHelpers.GenerateTimetable((IEnumerable <TimetableSlot>)value));
 }
        public DataClassTabItem(Type type)
        {
            InitializeComponent();
            DataType = type;
            void attachButtonCommand(ButtonBase item, CommandBinding binding, // local function to assist attaching commands
                                     object parameter = null)
            {
                item.CommandParameter = parameter;
                item.Command          = binding.Command;
                item.CommandBindings.Add(binding);
            }

            void attachMICommand(MenuItem item, CommandBinding binding, // local function to assist attaching commands
                                 object parameter = null)
            {
                item.CommandParameter = parameter;
                item.Command          = binding.Command;
                item.CommandBindings.Add(binding);
            }

            CommandBinding editBinding = new CommandBinding(DataGridCommands.EditItem, ExecuteEditItem, CanExecuteEditItem);
            CommandBinding newBinding  = new CommandBinding(DataGridCommands.NewItem, ExecuteNewItem, CanAlwaysExecute);
            CommandBinding dupBinding  = new CommandBinding(DataGridCommands.DuplicateItem, ExecuteDuplicateItem, CanExecuteDuplicateItem);
            CommandBinding delBinding  = new CommandBinding(DataGridCommands.DeleteItem, ExecuteDeleteItem, CanExecuteDeleteItem);

            attachMICommand(miEditItem, editBinding);
            attachMICommand(miNewItem, newBinding);
            attachMICommand(miDeleteItem, delBinding);     // attach commands to the context menu
            attachMICommand(miDuplicateItem, dupBinding);

            dgMainDataGrid.CommandBindings.Add(delBinding);
            dgMainDataGrid.InputBindings.Add(new KeyBinding(DataGridCommands.DeleteItem, new KeyGesture(Key.Delete)));

            attachButtonCommand(btNewToolbar, newBinding);
            attachButtonCommand(btEditToolbar, editBinding);
            attachButtonCommand(btDuplicateToolbar, dupBinding); // attach commands to the toolbar
            attachButtonCommand(btDeleteToolbar, delBinding);
            searchtimer.Elapsed += delegate(object sender, ElapsedEventArgs e)
            {
                Dispatcher.Invoke(() => RefreshFilter());
            };
            void handler(object sender, TextChangedEventArgs e)
            {
                searchtimer.Stop();
                searchtimer.Start();
            };
            filterName.TextChanged += handler;
            filterSh.TextChanged   += handler;
            cbRemove.Click         += delegate(object sender, RoutedEventArgs e) { RefreshFilter(); };
            Filtering <BaseDataClass> data = ((INotifyCollectionChanged)App.Data.FromType(type)).Casting <BaseDataClass>().Filtering(o => o.Visible);

            defaultView                = new ListCollectionView(data);
            data.CollectionChanged    += delegate(object sender, NotifyCollectionChangedEventArgs e) { defaultView.Refresh(); };
            dgMainDataGrid.ItemsSource = defaultView;
            dgMainDataGrid.Columns.Add(new DataGridTemplateColumn()
            {
                CanUserSort    = true,
                SortMemberPath = "Name",
                Width          = new DataGridLength(1, DataGridLengthUnitType.Star),
                MinWidth       = 20,
                Header         = "Name",
                CellTemplate   = (DataTemplate)Resources["NameTemplate"]
            });
            dgMainDataGrid.Columns.Add(new DataGridTemplateColumn()
            {
                CanUserSort    = true,
                SortMemberPath = "Shorthand",
                Width          = new DataGridLength(1, DataGridLengthUnitType.Star),
                MinWidth       = 20,
                Header         = "Shorthand",
                CellTemplate   = (DataTemplate)Resources["ShTemplate"]
            });
            System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(type.TypeHandle);
            int width = BaseDataClass.ExposedProperties[type].Count(prop => prop.Type.IsInterface <IList>());

            width = width == 1 ? 4 : width;
            foreach (CustomPropertyInfo prop in BaseDataClass.ExposedProperties[type])
            {
                bool                    islist       = prop.Type.IsInterface <IList>();
                DataTemplate            cellTemplate = new DataTemplate(type);
                FrameworkElementFactory tbFactory    = new FrameworkElementFactory(typeof(TextBlock));
                tbFactory.SetValue(StyleProperty, Resources["tbStyle"]);
                Binding binding = new Binding(prop.PropertyInfo.Name);
                if (prop.Type == typeof(ObservableCollectionExtended <TimetableSlot>))
                {
                    binding.Converter = new ListReportLength();
                    TextBlock tb = new TextBlock()
                    {
                        Text = "Loading..."
                    };
                    ToolTip tt = new ToolTip()
                    {
                        Content = tb
                    };
                    tbFactory.SetValue(ToolTipProperty, tt);
                    scrolltimer.Elapsed += delegate(object sender_i, ElapsedEventArgs e_i)
                    {
                        Dispatcher.Invoke(() =>
                        {
                            if (tt.IsOpen)
                            {
                                DateTime date = DateTime.Now;
                                tt.Content    = VisualHelpers.GenerateTimetable((IEnumerable <TimetableSlot>)prop.PropertyInfo.GetValue(tt.DataContext));
                            }
                        });
                    };
                    tt.Opened += delegate(object sender, RoutedEventArgs e)
                    {
                        if (!scrolltimer.Enabled)
                        {
                            tt.Content = VisualHelpers.GenerateTimetable((IEnumerable <TimetableSlot>)prop.PropertyInfo.GetValue(tt.DataContext));
                        }
                    };
                    tt.Closed     += delegate(object sender, RoutedEventArgs e) { tt.Content = tb; };
                    tb.MouseLeave += delegate(object sender, MouseEventArgs e) { tt.IsOpen = false; };
                }
                else if (islist)
                {
                    binding.Converter = new ListFormatter();
                }
                else
                {
                    binding.Converter = new PropertyConverter(prop);
                }
                tbFactory.SetBinding(TextBlock.TextProperty, binding);
                cellTemplate.VisualTree = tbFactory;

                dgMainDataGrid.Columns.SmartInsert(islist ? -1 : 2, new DataGridTemplateColumn()
                {
                    Width        = new DataGridLength(width, islist ? DataGridLengthUnitType.Star : DataGridLengthUnitType.Auto),
                    Header       = prop.Alias,
                    CellTemplate = cellTemplate
                });
            }
            dgMainDataGrid.MouseDoubleClick += delegate(object sender, MouseButtonEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed && dgMainDataGrid.SelectedItems.Count == 1)
                {
                    new InformationWindow((BaseDataClass)dgMainDataGrid.SelectedItem).Show();
                }
            };
            searches = BaseDataClass.SearchParameters.DefaultDictGet <Type, IList <SearchFactory>, List <SearchFactory> >(type).Select(sf => sf.GenerateSearch()).ToObservable();
            searches.CollectionChanged += SearchChanged;
            foreach (SearchBase search in searches)
            {
                wpAdvanced.Children.Add(search.GenerateUI());
            }
            dgMainDataGrid.UnselectAll();
        }