[SOLVED] Change Box ContentTemplate while selecting parts

Hi,

I’m using a custom DragSelectingTool to preview some shapes to be drawn when user releases mouse button. It works perfectly with rectangle and ellipse. I use Box.ContentTemplate property inside inherited CreateBox method.
I’m currently trying to preview lines, and I’m facing a problem:
I use Line1 figure, so that line is drawn from the upper left corner of the bow to the lower right one. It is ok when I drag mouse to the lower right or the upper left of the initial point. In the other cases, my line is not drawn as desired (because my start and end points are the two other corners).
If I use Line2 figure, it works for the last case but not for the 2 first.

So I try to detect in which case I am (inside DoMouseMove method) and I change Box.ContentTemplate depending on where my current mouse position is, depending on the initial one.
But result is weird, once I change content template, box size becomes (101;101) and never changes.

Do you have an idea on this problem, or an advice on how to make this differently ?

I hope to be clear enough, don’t hesitate if you need more information

Regards

Yes, there are multiple ways for a line to occupy a rectangular area.

Maybe you could override DoMouseMove to modify the Box. Compare Diagram.FirstMousePointInModel with Diagram.LastMousePointInModel.

Thanks for your reply

That’s exactly the way I currently detect which template I have to use (comparing FirstMousePointInModel and LastMousePointInModel and change Box.ContentTemplate if needed) but it doesn’t work (box size become fixed, 101px*101px once content template has changed…). I also tried to change Box.Content directly by instantiating myself the template, but in this case there is no visual change.

Another idea ?

I wasn’t thinking about modifying the template, but the actual Box. It might need to change repeatedlly as the mouse is moved around, so that is why you cannot depend on doing it in an override of CreateBox, but only in an override of DoMouseMove.

I didn’t reply but I managed to do what I wanted to. Here is my solution, hoping it can help someone else.
Modification of Box.ContentTemplate in DoMouseMove method is a bad idea. According to Walter’s answer, we have to change directly Box element.

Here is code of my DoMouseMove method:

var sizeX = Diagram.LastMousePointInModel.X - Diagram.FirstMousePointInModel.X;
var sizeY = Diagram.LastMousePointInModel.Y - Diagram.FirstMousePointInModel.Y;``

if ((sizeX >= 0 && sizeY >= 0) || (sizeX < 0 && sizeY < 0))
{    
    if (_isLine1) return;
    if (Box != null)
    {
        NodeShape nodeShape = (NodeShape) Box.ContentTemplate.FindName("Shape", Box); // "Shape" is the name of the NodeShape element in Box dataTemplate
        if (nodeShape != null)
        {
            _isLine1 = true;
            nodeShape.SetValue(NodePanel.FigureProperty, NodeFigure.Line1);
        }
    }
}
else
{
    if (!_isLine1) return;
    if (Box != null)
    {
        NodeShape nodeShape = (NodeShape) Box.ContentTemplate.FindName("Shape", Box); // "Shape" is the name of the NodeShape element in Box dataTemplate
        if (nodeShape != null)
        {
            _isLine1 = false;
            nodeShape.SetValue(NodePanel.FigureProperty, NodeFigure.Line2);
        }
    }
} 

The aim of this method is to change Node.Figure property selecting between Line1 and Line2 depending on how is drawn the rectangle selection (TopLeft to BottomRight, BottomLeft to TopRight, TopRight to BottomLeft, BottomRight to TopLeft).
_isLine1 boolean is used to know which figure is currently used, to avoid changing Figure property each time DoMouseMove method is called