private void Rectangle_Drop(object sender, DragEventArgs e)
{
i++;
myButton = new Button();
myButton.Content = "Drop Button";
myButton.Height = 25;
myButton.Width = 120;
myButton.Name = "simple";
myButton.VerticalAlignment = System.Windows.VerticalAlignment.Top;
myButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
myButton.Margin = new Thickness(coordinate.X + 12, coordinate.Y + 48, 0, 0);
myButton.Content = "Button" + i;
myButton.GotFocus += new RoutedEventHandler(myButton_GetFocus);
myGrid.Children.Add(myButton);
}
Динамическое создание объектов в WPF при событии drag and drop.
Код:
Почему-то событие вызывается 2 раза в место одного. Помогите плиз, может чего не понимаю?
Вот весь код:
Код:
namespace WPF_example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Button[] myButton= new Button[20];
Point coordinate;
int i=0;
public MainWindow()
{
InitializeComponent();
}
private bool isDragStarted;
private void Rectangle_Drop(object sender, DragEventArgs e)
{
i++;
myButton = new Button();
myButton.Content = "Drop Button";
myButton.Height = 25;
myButton.Width = 120;
myButton.Name = "simple";
myButton.VerticalAlignment = System.Windows.VerticalAlignment.Top;
myButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
myButton.Margin = new Thickness(coordinate.X + 12, coordinate.Y + 48, 0, 0);
myButton.Content = "Button" + i;
myButton.GotFocus += new RoutedEventHandler(myButton_GetFocus);
myGrid.Children.Add(myButton);
}
private void myButton_GetFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show(e.OriginalSource.ToString());
Button btn = (Button)e.OriginalSource;
btn.Content = "work";
}
private void testButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.isDragStarted = true;
}
}
private void testButton_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (this.isDragStarted)
{
DataObject data = new DataObject("Button");
DragDrop.DoDragDrop(this.testButton, data, DragDropEffects.Move);
this.isDragStarted = false;
}
}
private void Rectangle_DropOver(object sender, DragEventArgs e)
{
coordinate = e.GetPosition(myRectangle);
}
}
}
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private Button[] myButton= new Button[20];
Point coordinate;
int i=0;
public MainWindow()
{
InitializeComponent();
}
private bool isDragStarted;
private void Rectangle_Drop(object sender, DragEventArgs e)
{
i++;
myButton = new Button();
myButton.Content = "Drop Button";
myButton.Height = 25;
myButton.Width = 120;
myButton.Name = "simple";
myButton.VerticalAlignment = System.Windows.VerticalAlignment.Top;
myButton.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
myButton.Margin = new Thickness(coordinate.X + 12, coordinate.Y + 48, 0, 0);
myButton.Content = "Button" + i;
myButton.GotFocus += new RoutedEventHandler(myButton_GetFocus);
myGrid.Children.Add(myButton);
}
private void myButton_GetFocus(object sender, RoutedEventArgs e)
{
MessageBox.Show(e.OriginalSource.ToString());
Button btn = (Button)e.OriginalSource;
btn.Content = "work";
}
private void testButton_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this.isDragStarted = true;
}
}
private void testButton_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (this.isDragStarted)
{
DataObject data = new DataObject("Button");
DragDrop.DoDragDrop(this.testButton, data, DragDropEffects.Move);
this.isDragStarted = false;
}
}
private void Rectangle_DropOver(object sender, DragEventArgs e)
{
coordinate = e.GetPosition(myRectangle);
}
}
}