2
votes

I use a WPF to display a shape on an Office panel. I can size the shape when application is loading. But I would like to resize this shape if the user resize the panel containing the shape. My problem is margin defined at the beginning is not changed after loading, so the size keep its initial margin.

I have a UserControl containing the WPF Shape and the resize event handler:

private void UserControlA_Resize(object sender, EventArgs e)
{
        myWPF.SetSizeOfShape(sizeOfPanel); // I collect sizeOfPanel, and it is OK, it is changed when panel is resized
}

xaml of the WPF is:

<Border Name="myShape"  Background="blue" CornerRadius="8" Margin="10,126,139,199" />

the WPF .cs is:

public void SetSizeOfShape(int widthOfPanel)
{
      myShape.Margin = new Thickness(widthOfPanel/3, 100, widthOfPanel/6, 100);

SetSizeOfShape is called when the application is loading and the size is correctly set - but if size is changed, it is called again, but doesn't change the margin displayed.

Do you know what is wrong and how to correct it ?

----- EDIT ----- There is probably a problem with my event handler. Indeed, if I put the event with a button click, it works - but if I use the Resize (or sizeChanged) event it doesn't : the event is called, but there is no effect on the shape. Do you know how to solve it ?

1
How did you set up your resize handler?mnistic
private void UserControlA_Resize(object sender, EventArgs e) { myWPF.SetSizeOfShape(sizeOfPanel); // I collect sizeOfPanel, and it is OK, it is changed when panel is resized }. But it seems to work when I inspect with debuger, the SetSizeOfShape method has the correct value - it is just that change is not takenFreddlow
No I meant what event is it handling, did you do userControl.SizeChanged += UserControlA_Resize; or something else?mnistic
It is self generated code (by click on the event on the designer). In self generated code, there is : this.Resize += new System.EventHandler(this.UserControlA_Resize)Freddlow
It should be SizeChanged, if it's not working post all of the relevant code (XAML + C#)mnistic

1 Answers

0
votes

I don't see a Resize event for the Border Control. It can size to its contents. Maybe give us more detail about what you're trying to do.

public void MyMouseOver()
{
    Ellipse myShape = new Ellipse() { Width = 200, Height = 100, Stroke = Brushes.Yellow, };
    MyCanvas.Children.Add(myShape);
    Canvas.SetTop(myShape,10);
    Canvas.SetLeft(myShape,10);

    myShape.MouseEnter += MyShape_MouseEnter;
    myShape.MouseLeave += MyShape_MouseLeave;
}

private void MyShape_MouseLeave(object sender, MouseEventArgs e)
{
    ((Ellipse)sender).RenderTransform = new ScaleTransform(1, 1);    // return scale to normal
}

private void MyShape_MouseEnter(object sender, MouseEventArgs e)
{
    ((Ellipse)sender).RenderTransform = new ScaleTransform(1.1, 1.1, Width / 2, Height / 2);
}