Search code examples
c#wpfuielement

UIElement can't be changed inside different name space


EDIT: For new programmers to OOP just calling the class isn't enough if the syntax is looking for a specific variable type it will throw an error. This was the problem as pointed at the answers bellow, this line of code:

System.Windows.Controls.Canvas.SetLeft(Gate_list[Gate_list.Count - 1], Pos.X);

It has one fatal mistake and that is that the Canvas.SetLeft is looking for a canvas child class (in this case a rectangle) and a point. However I sent in a Class type Gate_list[Gate_list.Count - 1] when it should've been Gate_list[Gate_list.Count - 1].GetRect()

End of edit

I have a delegate that is calling a class in a method. The delegate is detecting a mouse down event on a Rectangle(Here is how it's done). In the method I'm trying to SetLeft on a Rectangle I just added to the Canvas but get the error CS1503.

I've tried converting it to System.Windows.UIElement but System can't be converted.

public partial class Program
{
    public void Rect_Button_MouseDown(MainWindow MainWind, string Tag)
    {
        Point Pos = new Point();
        Pos = System.Windows.Input.Mouse.GetPosition(MainWind.Main_Canvas);
        if (Drag == false)
        {
            Drag = true;
            Gate_list.Add(new Gate_Class(Convert.ToInt32(Tag),new Rectangle()));
            MainWind.Main_Canvas.Children.Add(Gate_list[Gate_list.Count-1].Get_Rect());
            System.Windows.Controls.Canvas.SetLeft(Gate_list[Gate_list.Count - 1], Pos.X);
        }
    }
}

I believe there should be an away to transfer system.windows but I don't know.

My question comes down to finding a way to convert Gate_list[] to UIElement.


Solution

  • Gate_Class is apparently not a UIElement. It should have a Rectangle property that returns the Rectangle that you pass to its constructor. You can then set the Canvas.Left property of the Rectangle:

    System.Windows.Controls.Canvas.SetLeft(Gate_list[Gate_list.Count - 1].Rectangle, Pos.X);
    

    public class Gate_Class
    {
        public Gate_Class(int tag, Rectangle rectangle)
        {
            //...
            Rectangle = rectangle;
        }
    
        public Rectangle Rectangle { get; }
    }