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
|
/**************************************DRAG*************************/
private void drageContenentControle(object sender, MouseButtonEventArgs e)
{
StackPanel Conteneur =new StackPanel();
// si l'utilisateur a cliqué sur le stackpanel
if (e.Source is StackPanel)
{
Conteneur = e.Source as StackPanel;
Conteneur.Background=Brushes.YellowGreen;
}
else{
// si l'utilisateur a cliqué sur l'image du composants
if(e.Source is Image)
{
Image img= e.Source as Image;
Conteneur= img.Parent as StackPanel;
Conteneur.Background=Brushes.YellowGreen;
}
// si l'utilisateur a cliqué sur le textBlock (le nom de composant)
else{
if (e.Source is TextBlock)
{
TextBlock text = e.Source as TextBlock;
Conteneur = text.Parent as StackPanel;
Conteneur.Background = Brushes.YellowGreen;
text.Background = Brushes.Red;
}
else {
}
}
}
DataObject data = new DataObject(typeof(StackPanel), Conteneur);
DragDrop.DoDragDrop(Conteneur, data, DragDropEffects.Copy);
}
/***************************DROP********************/
private void DropImage(object sender, DragEventArgs e)
{
StackPanel contr = e.Data.GetData(typeof(StackPanel)) as StackPanel;
contr.Background = null;
Image image = new Image();
TextBlock txt = new TextBlock();
foreach(Object obj in contr.Children){
if (obj is Image)
{
Image pic = obj as Image;
image.Source = pic.Source;
image.Width = pic.Width;
image.Height = pic.Height;
image.Name = pic.Name;
image.Margin=pic.Margin;
image.IsHitTestVisible = false;
}else {
if(obj is TextBlock)
{
TextBlock text = obj as TextBlock;
txt.Name = text.Name;
txt.Text = text.Text;
txt.Margin=text.Margin;
txt.IsHitTestVisible = true;
}
}
}
StackPanel stackControl = new StackPanel() {Width = contr.Width, Height = contr.Height,
Background = contr.Background, Name = contr.Name,
Children = {image,txt } };
/******************************************************************************************/
/****** ICI j'ajoute le stackPanel que je veux droper dans un ContentControl pour pouvoir lui appliquer une Template; **/
/*****************************************************************************************/
essaye = new ContentControl();
essaye.Width = contr.Width;
essaye.Height = contr.Height;
essaye.MinWidth = contr.Width - 10;
essaye.MinHeight = contr.Height - 10;
Selector.SetIsSelected(essaye,true);
essaye.SetResourceReference(StyleProperty, "DesignerItemStyle");
essaye.Content =stackControl;
Canvas.SetLeft(essaye, e.GetPosition(MYCANVAS).X);
Canvas.SetTop(essaye, e.GetPosition(MYCANVAS).Y);
MYCANVAS.Children.Add(essaye);
} |
Partager