我是wpf的新手,我想做的是为xaml添加3个按钮和一个画布。 每个按钮代表一种颜色。
所以我的问题是,我做错了什么?有比这更好的方法吗?
获取画布索引是否有比使用canvas.Children.Count;
更好的方法?
编辑:似乎使用索引有点乱,应该将它们存储在UIElement而不是
XAML
<StackPanel Background="Aqua">
<Button Click="ButtonA_Click" Width="80">putDOT_A</Button>
<Button Click="ButtonB_Click" Width="80">putDOT_B</Button>
<Button Click="ButtonC_Click" Width="80">putDOT_C</Button>
<Canvas
Name="canvas"
Panel.ZIndex="0"
Background="White"
ClipToBounds="true"
Width="400" Height="400"
MouseDown="Canvas_MouseDown" >
</Canvas>
</StackPanel>
CS
{
public int indexA = -1;
public int indexB = -1;
public MainWindow()
{
InitializeComponent();
}
private void RemoveAxisPoint(int index)
{
if (index >= 0)
{
canvas.Children.RemoveAt(index - 1);
}
}
private enum MyCurrentButton
{
Dot, A, B
}
private void Canvas_MouseDown(object sender, MouseButtonEventArgs e)
{
switch (curr)
{
case MyCurrentButton.A:
PutA(e);
break;
case MyCurrentButton.B:
PutB(e);
break;
case MyCurrentButton.Dot:
PutDot(e);
break;
default:
return;
}
}
private void PutDot(MouseButtonEventArgs e)
{
Dot dot = new Dot(canvas);
dot.Draw(e.GetPosition(canvas));
}
private void PutB(MouseButtonEventArgs e)
{
Dot dot = new Dot(canvas,0, 255, 0);
RemoveAxisPoint(indexB);
dot.DrawB(e.GetPosition(canvas));
indexB = canvas.Children.Count;
}
private void PutA(MouseButtonEventArgs e)
{
Dot dot = new Dot(canvas, 255, 255, 0);
RemoveAxisPoint(indexA);
dot.DrawA(e.GetPosition(canvas));
indexA = canvas.Children.Count;
}
private MyCurrentButton curr = MyCurrentButton.Dot;
private void ButtonA_Click(object sender, RoutedEventArgs e)
{
curr = MyCurrentButton.A;
}
private void ButtonB_Click(object sender, RoutedEventArgs e)
{
curr = MyCurrentButton.B;
}
private void ButtonC_Click(object sender, RoutedEventArgs e)
{
curr = MyCurrentButton.Dot;
}
class Dot : IDraw
{
private readonly Canvas _canvas;
public Ellipse dot { get; private set; }
public double Width { get; set; }
public double Height { get; set; }
public Brush Fill { get; private set; }
public Dot(Canvas canvas, byte r = 0, byte g = 0, byte b = 0)
{
_canvas = canvas;
SolidColorBrush color = new SolidColorBrush(Color.FromRgb(r, g, b));
dot = new Ellipse
{
Width = 10,
Height = 10,
Fill = color
};
}
public void DrawA(Point location)
{
Canvas.SetLeft(dot, location.X - (dot.Width / 2));
Canvas.SetTop(dot, location.Y - (dot.Height / 2));
_canvas.Children.Add(dot);
}
public void DrawB(Point location)
{
Canvas.SetLeft(dot, location.X - (dot.Width / 2));
Canvas.SetTop(dot, location.Y - (dot.Height / 2));
_canvas.Children.Add(dot);
}
public void Draw(Point location)
{
Canvas.SetLeft(dot, location.X - (dot.Width / 2));
Canvas.SetTop(dot, location.Y - (dot.Height / 2));
_canvas.Children.Add(dot);
}
}
public interface IDraw
{
void Draw(Point location);
}
}
答案 0 :(得分:1)
您的代码不起作用的原因是,在删除第一个索引后,第二个索引(如果大于第一个索引)是错误的。
所以要解决这个问题......
RemoveAxisPoint(indexA);
if(indexB > indexA)indexB--;
和...
RemoveAxisPoint(indexB);
if(indexA > indexB)indexA--;
所以这将解决当前的问题。但是你的设计非常脆弱,一旦你改变它就会再次破裂。我建议使用Canvas.Children.Remove()而不是RemoveAt(),这会略微降低性能,但会大大提高健壮性。