我的用户控件中有两个面板(Canvas),左侧画布和右侧画布。现在,在Left canvas上,我首先加载一个Image,然后在MouseLeftButton上点击我正在绘制一个Ellipse。因此,当我在Left Canvas上绘制Ellipse时,我必须在Right画布上使用TextBox绘制相同的Ellipse。 请建议我如何实现这一目标。 我尝试使用类的相同对象(DrawEllipse),但无法在Right Canvas上添加子项。
谢谢, Prakhar
答案 0 :(得分:0)
XAML:忽略组框
<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>
代码背后:
#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
希望这有帮助..这只是一个简单的方法..你可以随意操纵它:))