Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何在MouseLeftButton上创建相同的几何体单击两个画布?_Wpf_Canvas - Fatal编程技术网

Wpf 如何在MouseLeftButton上创建相同的几何体单击两个画布?

Wpf 如何在MouseLeftButton上创建相同的几何体单击两个画布?,wpf,canvas,Wpf,Canvas,我的用户控件中有两个面板(画布),左画布和右画布。现在,在左边的画布上,我首先加载一个图像,然后在MouseLeftButton上单击,我绘制了一个椭圆。所以当我在左画布上画椭圆时,我必须在右画布上用文本框画同一个椭圆。 请建议我如何做到这一点。 我尝试使用类的同一对象(Dropellipse),但无法将子对象添加到正确的画布上 谢谢, PrakarXAML:忽略groupbox <Grid> <Grid.ColumnDefinitions> &l

我的用户控件中有两个面板(画布),左画布和右画布。现在,在左边的画布上,我首先加载一个图像,然后在MouseLeftButton上单击,我绘制了一个椭圆。所以当我在左画布上画椭圆时,我必须在右画布上用文本框画同一个椭圆。 请建议我如何做到这一点。 我尝试使用类的同一对象(Dropellipse),但无法将子对象添加到正确的画布上

谢谢,
Prakar

XAML:忽略groupbox

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition />
    </Grid.RowDefinitions>
    <GroupBox Grid.Row="0" Grid.Column="0" Margin="-2,45,0,0" BorderThickness="0" BorderBrush="Transparent">
    </GroupBox>

    <Canvas Grid.Row="1" Grid.Column="0" Name="LeftCanvas" Background="SaddleBrown" MouseLeftButtonUp="LeftCanvas_MouseLeftButtonUp">

    </Canvas>
    <Canvas Grid.Row="1" Grid.Column="1" Name="RightCanvas" Background="PaleGoldenrod">

    </Canvas>
</Grid>

希望这有帮助。。这只是一个简单的方法。。你可以随心所欲地操作它:)

Thanx Max。如果我这样做,椭圆之间的通信可能吗?。我的意思是,如果我从左画布中的第二个椭圆反转第一个椭圆的位置,我可以将这个重新排序传递到右画布吗?。谢谢你的帮助!我不太清楚你的意思,不客气。我只是想解释得更清楚些!假设在左侧画布上创建的椭圆具有数字1和2。正如您所提到的,同样的椭圆也将在右侧画布上创建。但在那之后,当我用鼠标移动1和2的位置时(比如对于一些矢量图像而不是椭圆)。在左画布上进行重新排序时,有没有办法在右画布上反转相同的顺序。然后,在您绘制的椭圆和自动创建的克隆之间创建链接是个好主意。然后我认为如果你想在画布上拖拽你的物品,并且想让另一个跟随,你应该做一个移动事件。etc.MoveEllipse\u MouseMove(对象发送者,MouseEventArgs e)//将椭圆从当前位置移动到新的MouseMove位置//获取正在拖动的椭圆的克隆//移动克隆相同的坐标,但在右侧画布上。3种方法:)如果要交换椭圆1和2的位置。这是同样的想法//OnSwap()//将左侧画布上的椭圆重新绘制/移动到正确位置//获取已移动椭圆的克隆//在右侧画布上重新绘制/移动已克隆椭圆希望这有所帮助。
#region Control events
private void LeftCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
  DrawEclipse(e.GetPosition(LeftCanvas));
}
#endregion

#region Private methods
private void DrawEclipse(Point position)
{
  // Create a red Ellipse.
  Ellipse LeftEllipse = new Ellipse();

  // Create a SolidColorBrush with a red color to fill the  
  // Ellipse with.
  SolidColorBrush mySolidColorBrush = new SolidColorBrush();

  // Describes the brush's color using RGB values.  
  // Each value has a range of 0-255.
  mySolidColorBrush.Color = Color.FromArgb(255, 255, 255, 0);
  LeftEllipse.Fill = mySolidColorBrush;
  LeftEllipse.StrokeThickness = 2;
  LeftEllipse.Stroke = Brushes.Black;

  // Set the width and height of the Ellipse.
  LeftEllipse.Width = 80;
  LeftEllipse.Height = 50;
  LeftCanvas.Children.Add(LeftEllipse);

  Ellipse RightEllipse = CloneEllipse(LeftEllipse);
  RightCanvas.Children.Add(RightEllipse);

  TextBox tb = new TextBox();
  tb.Width = 50;
  RightCanvas.Children.Add(tb);

  Canvas.SetTop(LeftEllipse, position.Y - (LeftEllipse.Height / 2));
  Canvas.SetLeft(LeftEllipse, position.X - (LeftEllipse.Width / 2));
  Canvas.SetTop(RightEllipse, position.Y - (RightEllipse.Height / 2));
  Canvas.SetLeft(RightEllipse, position.X - (RightEllipse.Width / 2));
  Canvas.SetTop(tb, position.Y + 5);
  Canvas.SetLeft(tb, position.X + 5);
}

private Ellipse CloneEllipse(Ellipse LeftEllipse)
{
  Ellipse EllipseClone = new Ellipse();

  EllipseClone.Fill = LeftEllipse.Fill;
  EllipseClone.StrokeThickness = LeftEllipse.StrokeThickness;
  EllipseClone.Stroke = LeftEllipse.Stroke;

  // Set the width and height of the Ellipse.
  EllipseClone.Width = LeftEllipse.Width;
  EllipseClone.Height = LeftEllipse.Height;

  return EllipseClone;
}
#endregion