0

i have random 2 to 6 image controls dynamically added, and for each i have dictionary/array containing data for that control, i.e x1, x2, y1, y1, ans.

How can i assign or bind the array to each image control?

basically i am making a drag and drop feature, and for that i have used canvas and placed the images in it. so when i drag image1 to area rect_1(space to drop the image1) i want to test image1.ans which contains a number i.e 1 to match against rect_1.ans, to see if image1 is at right rect to mark as correct placement.

i have searched but didn't found how to achive my situation, This post uses xaml but i need to make it in code behind. please guide me on this.

This is code to add image1 For now i am not dynamically creating dictionary

_draggedImageData = new Dictionary<string, int>
{
    {"x1", 11},
    {"x2", 144},
    {"y1", 123},
    {"y2", 143},
    {"ss", 0},
    {"a", 0}
};
_draggedImageData["width"] = (int)Math.Sqrt(Math.Pow(_draggedImageData["x2"] - _draggedImageData["x1"], 2) + Math.Pow(_draggedImageData["y2"] - _draggedImageData["y1"], 2)); //Math.Sqrt( Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))
_draggedImageData["height"] = _draggedImageData["y2"] - _draggedImageData["y1"]; // y2-y1

// Create a CroppedBitmap.
_cb = new CroppedBitmap(bitmapImage, new Int32Rect(_draggedImageData["x1"], _draggedImageData["y1"], _draggedImageData["width"], _draggedImageData["height"])); //x1, y1 : select region rect

var croppedImage = new Image{Source = _cb};
Canvas.SetLeft(croppedImage, _draggedImageData["x1"]); //x1
Canvas.SetTop(croppedImage, _draggedImageData["y1"]); //y1
_canvas.Children.Add(croppedImage);

this is code to check if its on same position.

private void CanvasMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
    if (_draggedImage == null) return;

    var position = e.GetPosition(_canvas);
    //<m x1="332" x2="465" y1="68" y2="86" ss="1" a="0"></m>
        _draggedImageData2 = new Dictionary<string, int>
        {
            {"x1", 332},
            {"x2", 465},
            {"y1", 68},
            {"y2", 86},
            {"ss", 1},
            {"a", 0}
        };
        _draggedImageData2["width"] = (int)Math.Sqrt(Math.Pow(_draggedImageData2["x2"] - _draggedImageData2["x1"], 2) + Math.Pow(_draggedImageData2["y2"] - _draggedImageData2["y1"], 2)); //Math.Sqrt( Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2))
        _draggedImageData2["height"] = _draggedImageData2["y2"] - _draggedImageData2["y1"]; // y2-y1


    var myRectangle = new Rect { Location = new Point(Canvas.GetLeft(_draggedImage), Canvas.GetTop(_draggedImage)), Size = new Size(_draggedImageData["width"], _draggedImageData["height"]) };
    var myRectangle2 = new Rect { Location = new Point(_draggedImageData2["x2"], _draggedImageData2["y2"]), Size = new Size(_draggedImageData2["width"], _draggedImageData2["height"]) };
    var doesIntersect = myRectangle.IntersectsWith(myRectangle2);


    if (doesIntersect)
    {
        var croppedImage = new Image
        {
            Source = _cb,
            Margin = new Thickness(_draggedImageData2["x1"], _draggedImageData2["y1"], 0.0, 0.0)
        };
        _canvas.Children.Add(croppedImage);

       //here i want to test if (draggedImageData2["a"] == myRectangle2["a"]) qArr[0].ans = 1; but i don't have myRectangle2 with dictionary values attached.
    }

    Canvas.SetLeft(_draggedImage, _draggedImageData["x1"]);
    Canvas.SetTop(_draggedImage, _draggedImageData["y1"]);

    _canvas.ReleaseMouseCapture();
    _draggedImage = null;
}
Community
  • 1
  • 1
ADi
  • 219
  • 1
  • 5
  • 17
  • You should maybe add a code sample of what you've already got. It clarifies your questions and shows effort. – i3arnon Jan 01 '14 at 02:26
  • It would also help if you clarify why you think you need to do it in code behind. I have never once seen a WPF problem that actually needed to be programmed in code-behind despite many, many people coming up with all sorts of reasons why they think they should. In every single case either the person didn't actually understand WPF or were too lazy to learn how to use it properly. – Mark Feldman Jan 01 '14 at 03:36
  • 1
    Sorry for not mentioning, basically the view is generated in canvas dynamically could be canvas, could be radio buton or checkbox or a webbowser on runtime. no control is added in XAML. i just want to attach some array to my controls so i can intract with that control according to the values in it, those values show which control to add i.e `type=radiobutton` make radio button etc. – ADi Jan 01 '14 at 03:44

2 Answers2

1

Use 'Tag' object property of control and add custom data to it, but Rect dont have Tag property.

_draggedImageData = new Dictionary<string, int>
{
    {"x1", 11},
    {"x2", 144},
    {"y1", 123},
    {"y2", 143},
    {"ss", 0},
    {"a", 0}
};

var croppedImage = new Image{Source = _cb};
croppedImage.Tag = _draggedImageData;

var _draggedImageData3 = new Dictionary<string, int>();
_draggedImageData3 = (Dictionary<string, int>)croppedImage.Tag;
0

Thanks for the clarification above. It might help if you have a look at an answer I gave a few weeks ago to someone who was trying to draw a chess board. Essentially the problem is the same i.e. trying to render a list of images on a canvas. I personally would create a class to encapsulate all those fields in your dictionary, but if you really do need a dictionary then that will work too and you just adapt your XAML bindings to use a dictionary instead.

Community
  • 1
  • 1
Mark Feldman
  • 15,731
  • 3
  • 31
  • 58
  • thanks for the link, its really informative. I'm trying to understand it to encapsulate to my needs. will be back after done thanks –  Jan 01 '14 at 04:21
  • 1
    No problem. I'm more than happy to provide actual code if you're at all interested, just let me know exactly which parts of the code you need to keep as they are. Either way good luck! – Mark Feldman Jan 01 '14 at 08:30
  • 1
    Felman thanks for ur help, i got it working for now. for now busy completing this assignment. will see your program later when get free. Thanks a lot :) –  Jan 02 '14 at 12:56