public override void OnApplyTemplate() { base.OnApplyTemplate(); if (this.isTemplateApplied) return; this.nodeProxy = DataContext as INodeProxy; if (nodeProxy == null) { throw new Exception("The DataContext was not a valid NodeProxy"); } this.imagePart = GetTemplateChild(NodeControl.ImagePartName) as Rectangle; if (this.imagePart != null) { this.imagePart.SetValue(Canvas.LeftProperty, 0.0); this.imagePart.SetValue(Canvas.TopProperty, 0.0); ImageBrush nodeImageBrush = this.imagePart.Fill as ImageBrush; if (nodeImageBrush != null) { nodeImageBrush.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(nodeImageBrush_ImageFailed); } } this.playMediaImagePart = GetTemplateChild(NodeControl.PlayMediaImagePartName) as Rectangle; if (this.playMediaImagePart != null) { this.playMediaImagePart.MouseLeftButtonDown += new MouseButtonEventHandler(playMediaImagePart_MouseLeftButtonUp); MetadataContext videoSourceKey = new MetadataContext() { MetadataName = "Video.Source", NodeUid = nodeProxy.Id }; if (nodeProxy.HasMetadata(videoSourceKey) && !string.IsNullOrEmpty(nodeProxy.GetNodeMetadata(videoSourceKey).MetadataValue)) { this.playMediaImagePart.Visibility = System.Windows.Visibility.Visible; } else { this.playMediaImagePart.Visibility = System.Windows.Visibility.Collapsed; } ImageBrush brush = playMediaImagePart.Fill as ImageBrush; if (brush != null) { brush.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(brush_ImageFailed); } } this.pauseMediaImagePart = GetTemplateChild(NodeControl.PauseMediaImagePartName) as Rectangle; if (this.pauseMediaImagePart != null) { this.pauseMediaImagePart.MouseLeftButtonDown += new MouseButtonEventHandler(pauseMediaImagePart_MouseLeftButtonUp); this.pauseMediaImagePart.Visibility = System.Windows.Visibility.Collapsed; ImageBrush brush = pauseMediaImagePart.Fill as ImageBrush; if (brush != null) { brush.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(brush_ImageFailed); } } this.textBlockPart = GetTemplateChild(NodeControl.TextBlockPartName) as TextBlock; this.textBlockBorderPart = GetTemplateChild(NodeControl.TextBlockBorderName) as Border; this.textBoxPart = GetTemplateChild(NodeControl.TextBoxPartName) as TextBox; this.transCntTextBlockPart = GetTemplateChild(NodeControl.TranscludionsCountTextBlockPartName) as TextBlock; this.childCntTextBlockPart = GetTemplateChild(NodeControl.ChildCountTextBlockPartName) as TextBlock; this.noteTextBlockPart = GetTemplateChild(NodeControl.NoteTextBlockPartName) as TextBlock; Rect textLocation = GetTextLocation(); if (this.textBlockBorderPart != null) { this.textBlockBorderPart.SetValue(Canvas.LeftProperty, textLocation.X); this.textBlockBorderPart.SetValue(Canvas.TopProperty, textLocation.Y); this.textBlockBorderPart.Width = textLocation.Width; this.textBlockBorderPart.Height = textLocation.Height; } if (this.textBlockPart != null) { this.textBlockPart.Width = textLocation.Width; this.textBlockPart.Height = textLocation.Height; if (this.textBlockBorderPart != null) { if (textLocation.Height < this.textBlockPart.ActualHeight) { this.textBlockBorderPart.Height = this.textBlockPart.ActualHeight; this.textBlockPart.Height = this.textBlockPart.ActualHeight; } else { if (this.textBlockPart.ActualHeight > 10) { this.textBlockBorderPart.Height = this.textBlockPart.ActualHeight; this.textBlockPart.Height = this.textBlockPart.ActualHeight; } } } this.textBlockHeight = this.textBlockPart.ActualHeight; this.textBlockWidth = this.textBlockPart.ActualWidth; this.textBlockPart.MouseLeftButtonDown += new MouseButtonEventHandler(OnNodeTextBlockMouseLeftButtonDown); } if (this.textBoxPart != null) { this.textBoxPart.SetValue(Canvas.LeftProperty, textLocation.X); this.textBoxPart.SetValue(Canvas.TopProperty, textLocation.Y); this.textBoxPart.Width = textLocation.Width + 5; this.textBoxPart.Height = textLocation.Height + 5; this.textBoxPart.KeyDown += new KeyEventHandler(TextBoxPart_KeyDown); } if (this.transCntTextBlockPart != null) { if (this.transCntTextBlockPart.Text == "1") { this.transCntTextBlockPart.Visibility = Visibility.Collapsed; } else { this.transCntTextBlockPart.Visibility = Visibility.Visible; } } if (this.childCntTextBlockPart != null) { if (nodeProxy.NodeType.Name == "CompendiumMapNode") { this.childCntTextBlockPart.Visibility = Visibility.Visible; this.childCntTextBlockPart.Text = GetMapNodeChildCount(nodeProxy); } else { this.childCntTextBlockPart.Visibility = Visibility.Collapsed; } } if (this.noteTextBlockPart != null) { MetadataContext noteKey = new MetadataContext() { MetadataName = "Note", NodeUid = nodeProxy.Id }; if (nodeProxy.HasMetadata(noteKey) && !string.IsNullOrEmpty(nodeProxy.GetNodeMetadata(noteKey).MetadataValue)) { this.noteTextBlockPart.Visibility = Visibility.Visible; ToolTipService.SetToolTip(this.noteTextBlockPart, nodeProxy.GetNodeMetadata(noteKey).MetadataValue); } else { this.noteTextBlockPart.Visibility = Visibility.Collapsed; } } BitmapImage nodeImg = new BitmapImage(); //for optimal memory usage and speed only create as many ImageBrush objects as there are images. if (ImageUrl != null) { if (!nodeImageCache.ContainsKey(ImageUrl)) { nodeImg.UriSource = new Uri(ImageUrl); ImageBrush imageBrush = new ImageBrush(); imageBrush.ImageSource = nodeImg; NodeImage = imageBrush; nodeImageCache.Add(ImageUrl, imageBrush); } else { NodeImage = nodeImageCache[ImageUrl]; } } else { if (!string.IsNullOrEmpty(NodeImageName)) { switch (NodeImageName) { case "Map": nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/network.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; case "Pro": nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/plus.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; case "Con": nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/minus.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; case "Decision": nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/generic.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; case "Idea": nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/exclamation.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; case "Question": nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/question.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; default: nodeImg.UriSource = new Uri("/SilverlightMappingToolBasic;component/Images/generic.png", UriKind.Relative); NodeImage = new ImageBrush() { ImageSource = nodeImg }; break; } } } this.okButtonPart = GetTemplateChild(NodeControl.OkButtonPartName) as Button; this.cancelButtonPart = GetTemplateChild(NodeControl.CancelButtonPartName) as Button; if (this.textBlockBorderPart != null && this.okButtonPart != null && this.cancelButtonPart != null) { double left = (double)textBlockBorderPart.GetValue(Canvas.LeftProperty); double top = (double)textBlockBorderPart.GetValue(Canvas.TopProperty); this.okButtonPart.SetValue(Canvas.LeftProperty, left + this.textBoxPart.Width - 100); this.okButtonPart.SetValue(Canvas.TopProperty, top + this.textBoxPart.Height); this.okButtonPart.Click += new RoutedEventHandler(OnNodeTextBoxOkClick); this.cancelButtonPart.SetValue(Canvas.LeftProperty, left + this.textBoxPart.Width - 50); this.cancelButtonPart.SetValue(Canvas.TopProperty, top + this.textBoxPart.Height); this.cancelButtonPart.Click += new RoutedEventHandler(OnNodeTextBoxCancelClick); } if (InEditState) { GoToState(NodeControl.EdittingStateName, false); if (this.textBoxPart != null) { this.textBoxPart.Focus(); } } else { this.GoToState(NodeControl.NormalStateName, false); } this.isTemplateApplied = true; }
private void NCellMouseMove(object sender, NCellMouseEventArgs args) { if (args.MouseEventArgs.LeftButton == MouseButtonState.Pressed && _mouseDownNCell != null) { var currentNCell = GetNCellFromMousePosition(args.MouseEventArgs.GetPosition(this)); if (currentNCell != null && !currentNCell.Equals(_mouseDownNCell) && !currentNCell.Equals(_lastMouseOverNCell)) { var mouseDownNCellRow = (int)_mouseDownNCell.GetValue(Grid.RowProperty); var mouseDownNCellColumn = (int)_mouseDownNCell.GetValue(Grid.ColumnProperty); var currentNCellRow = (int)currentNCell.GetValue(Grid.RowProperty); var currentNCellColumn = (int)currentNCell.GetValue(Grid.ColumnProperty); //计算绘制选中效果的矩形区域的起始点 Point startPoint, endPoint; if (currentNCellRow >= mouseDownNCellRow) { if (currentNCellColumn >= mouseDownNCellColumn) { startPoint = _mouseDownNCell.TranslatePoint(new Point(), NCellContainer); endPoint = currentNCell.TranslatePoint(new Point(currentNCell.ActualWidth, currentNCell.ActualHeight), NCellContainer); } else { startPoint = _mouseDownNCell.TranslatePoint(new Point(_mouseDownNCell.ActualWidth, 0), NCellContainer); endPoint = currentNCell.TranslatePoint(new Point(0, currentNCell.ActualHeight), NCellContainer); } } else { if (currentNCellColumn >= mouseDownNCellColumn) { startPoint = _mouseDownNCell.TranslatePoint(new Point(0, _mouseDownNCell.ActualHeight), NCellContainer); endPoint = currentNCell.TranslatePoint(new Point(currentNCell.ActualWidth, 0), NCellContainer); } else { startPoint = _mouseDownNCell.TranslatePoint(new Point(_mouseDownNCell.ActualWidth, _mouseDownNCell.ActualHeight), NCellContainer); endPoint = currentNCell.TranslatePoint(new Point(0, 0), NCellContainer); } } //设置选中效果 var leftMargin = Math.Min(startPoint.X, endPoint.X); var topMargin = Math.Min(startPoint.Y, endPoint.Y); _effectBorder = new Border() { BorderThickness = new Thickness(2), BorderBrush = Brushes.Blue }; _effectBorder.SetValue(Canvas.LeftProperty, leftMargin); _effectBorder.SetValue(Canvas.TopProperty, topMargin); _effectBorder.Width = Math.Abs(startPoint.X - endPoint.X); _effectBorder.Height = Math.Abs(startPoint.Y - endPoint.Y); _effectBorder.Background = Brushes.SkyBlue; _effectBorder.Opacity = 0.3; //当合并项处于选中边界时 var selectedRect = NTableUtils.BuildRect(startPoint, endPoint); //标识经过合并项的处理,选中区域是否已改变,若已改变则重新扫描,是否有合并项处于边界 bool isSelectionRectChanged; do { isSelectionRectChanged = false; foreach (var mergeItem in _nTableData.MergeCollection) { var mergeItemRect = NTableUtils.TranslateBounds(mergeItem.RootNCell, NCellContainer); if (NTableUtils.IsRectIntersect(selectedRect, mergeItemRect)) { if (mergeItemRect.Y < selectedRect.Y) { _effectBorder.SetValue(Canvas.TopProperty, mergeItemRect.Y); _effectBorder.Height += selectedRect.Y - mergeItemRect.Y; } if (mergeItemRect.X < selectedRect.X) { _effectBorder.SetValue(Canvas.LeftProperty, mergeItemRect.X); _effectBorder.Width += selectedRect.X - mergeItemRect.X; } if (mergeItemRect.Right > selectedRect.Right) { _effectBorder.Width += mergeItemRect.Right - selectedRect.Right; } if (mergeItemRect.Bottom > selectedRect.Bottom) { _effectBorder.Height += mergeItemRect.Bottom - selectedRect.Bottom; } var newSelectionRect = new Rect((double)_effectBorder.GetValue(Canvas.LeftProperty), (double)_effectBorder.GetValue(Canvas.TopProperty), _effectBorder.Width, _effectBorder.Height); if (newSelectionRect != selectedRect) { selectedRect = newSelectionRect; isSelectionRectChanged = true; } } } } while (isSelectionRectChanged); //实时更新选中的单元格集合 _selectedNCells.Clear(); var rows = _nTableData.Rows.Count; var columns = _nTableData.Columns.Count; for (var i = 0; i < rows; i++) { for (var j = 0; j < columns; j++) { if (NTableUtils.IsRectIntersect(NTableUtils.TranslateBounds((FrameworkElement)NCellContainer.Children[i * columns + j], NCellContainer), selectedRect)) { _selectedNCells.Add((NCell)NCellContainer.Children[i * columns + j]); } } } for (var i = 0; i < _selectedNCells.Count; i++) { if (_selectedNCells[i].MergeId != Guid.Empty) { var mergeItem = _nTableData.MergeCollection.First(x => x.Id == _selectedNCells[i].MergeId); mergeItem.ToList().ForEach(x => { if (!_selectedNCells.Contains(x)) { _selectedNCells.Add(x); } }); } } ClearSelection(); EffectContainer.Children.Add(_effectBorder); } _lastMouseOverNCell = currentNCell; } }