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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Xml;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace essayeService
{
public partial class DesignerCanvas : Canvas
{
private Point? rubberbandSelectionStartPoint = null;
private SelectionService selectionService;
internal SelectionService SelectionService
{
get
{
if (selectionService == null)
selectionService = new SelectionService(this);
return selectionService;
}
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.Source == this)
{
// in case that this click is the start of a
// drag operation we cache the start point
// sa c pour la selection dans la canvas"" dans le vide du canvas ""
this.rubberbandSelectionStartPoint = new Point?(e.GetPosition(this));
// if you click directly on the canvas all
// selected items are 'de-selected'
SelectionService.ClearSelection();
Focus();
e.Handled = true;
}
}
/******************************************************/
/* methode pour pouvoir selectionner dans le vide ****/
/****************************************************/
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
// if mouse button is not pressed we have no drag operation, ...
if (e.LeftButton != MouseButtonState.Pressed)
this.rubberbandSelectionStartPoint = null;
// ... but if mouse button is pressed and start
// point value is set we do have one
if (this.rubberbandSelectionStartPoint.HasValue)
{
// create rubberband adorner
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this);
if (adornerLayer != null)
{
RubberbandAdorner adorner = new RubberbandAdorner(this, rubberbandSelectionStartPoint);
if (adorner != null)
{
adornerLayer.Add(adorner);
}
}
}
e.Handled = true;
}
/***********************************************************************************************************************/
/***** c'est dans cette methode que je veux modifier mon controle template pour lui rajouter des connecteur ***********/
/*********************************************************************************************************************/
protected override void OnDrop(DragEventArgs e)
{
/***************************************************************************************************************/
/* ça c'est que je fait maintenant mais sans modifier le controle template pour le connector qui est definie dans
* DesigneItem.xaml,
/*****************************************************************************************************************/
Grid ss = new Grid();
ss.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.5, GridUnitType.Star) });
ss.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.5, GridUnitType.Star) });
TextBlock v = new TextBlock();
v.Text = "Test";
Grid.SetRow(v,1);
ss.Children.Add(v);
v.VerticalAlignment = VerticalAlignment.Center;
DesignerItem newItem = null;
newItem = new DesignerItem();
newItem.Height = 100;
newItem.Width = 100;
newItem.SetResourceReference(StyleProperty, "DesignStyle");
ss.Background = Brushes.Gray;
ss.IsHitTestVisible = false;
newItem.Content = ss;
SetConnectorDecoratorTemplate(newItem);
Canvas.SetLeft(newItem,50);
Canvas.SetTop(newItem, 50);
this.Children.Add(newItem);
/*************************************************************************************************************************/
/*ici c'est le code que j'ai essayé pour modifier le controle template mais sa ne fonctionne pas car lorsque je cherche
* la Grid pour la modifier en ajoutant des Conntector, sa donnr une exception
/*****************************************************************************************************************/
/*
DesignerItem newItem = null;
newItem = new DesignerItem();
newItem.SetResourceReference(StyleProperty, "DesignStyle");
Grid w = newItem.Template.FindName("PART_LesPointDeConnexion", newItem) as Grid; // ici le systeme bloque et je recois null dans la grid w
MessageBox.Show("" + w);
w.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.5, GridUnitType.Star) });
w.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.5, GridUnitType.Star) });
Connector cn = new Connector();
cn.Style = this.FindResource("styleCon") as Style;
cn.VerticalAlignment = VerticalAlignment.Bottom;
cn.HorizontalAlignment = HorizontalAlignment.Left;
w.Children.Add(cn);
Grid.SetColumn(cn, 0);
Grid.SetRow(cn, 1);
ss.Background = Brushes.Green;
ss.IsHitTestVisible = false;
newItem.Content = ss;
SetConnectorDecoratorTemplate(newItem);
Canvas.SetLeft(newItem,50);
Canvas.SetTop(newItem, 50);
this.Children.Add(newItem);
*/
}
protected override Size MeasureOverride(Size constraint)
{
Size size = new Size();
foreach (UIElement element in this.InternalChildren)
{
double left = Canvas.GetLeft(element);
double top = Canvas.GetTop(element);
left = double.IsNaN(left) ? 0 : left;
top = double.IsNaN(top) ? 0 : top;
//measure desired size for each child
element.Measure(constraint);
Size desiredSize = element.DesiredSize;
if (!double.IsNaN(desiredSize.Width) && !double.IsNaN(desiredSize.Height))
{
size.Width = Math.Max(size.Width, left + desiredSize.Width);
size.Height = Math.Max(size.Height, top + desiredSize.Height);
}
}
// add margin
size.Width += 10;
size.Height += 10;
return size;
}
private void SetConnectorDecoratorTemplate(DesignerItem item)
{
if (item.ApplyTemplate() && item.Content is UIElement)
{
ControlTemplate template = DesignerItem.GetConnectorDecoratorTemplate(item.Content as UIElement);
Control decorator = item.Template.FindName("PART_ConnectorDecorator", item) as Control;
if (decorator != null && template != null)
decorator.Template = template;
}
}
}
} |
Partager