locked
Detect when a usercontrol is resized? RRS feed

  • General discussion

  • I'm trying to find some way to respond to a resize event from within a UserControl. Without knowing anything about its parent container or its position in same, the usercontrol needs to update its layout when its own height/width changes. However, I cannot override height/width, and there is no resize event that I can find.

    For now, I'm hacking around it with a Refresh() method, but that is very un-Silverlight.

    Responding to the browser resize event is ok for now, but really limits what I can do with this in the future.

    Suggestions?

    Pete

    Tuesday, October 30, 2007 12:32 PM

All replies

  • Hey Pete,

    I'm sure you probably have, but have you looked at Dave Relyea's layout framework library?

    Tuesday, October 30, 2007 1:21 PM
  • Thanks.

    He has some great stuff in there, but I don't see anything that addresses this issue. The fact that he doesn't address it could be telling :)

    Pete

    Tuesday, October 30, 2007 1:39 PM
  • I solved this by implementing an interface on all controls (and pages) then I pass the parent into the user control (a page or an other control), then attach to it's resize event (which is defined on the interface).

    So, when a page resizes, all the child controls (and it's controls) will get the event from the parent.

     Roger Larsen.

    Tuesday, October 30, 2007 1:52 PM
  • Thanks Roger.

    Looks like the only way to do it currently is to require intimate knowledge of containing controls (I have usercontrols on usercontrols).

    I'll stick with my Refresh hack until MSFT comes out with an updated control model. Hopefully it will have some way of handling that.

    Pete

    Tuesday, October 30, 2007 2:03 PM
  • If control.Parent would work properly we would have solved it easier.

     It seems odd to me that I still spend as much time on resizing issues as I used to do with html. I hoped it would be easier.

    Roger Larsen

    Tuesday, October 30, 2007 4:45 PM
  • I'm sure it will change for the easier, post-alpha :)

    Pete

    Tuesday, October 30, 2007 8:04 PM
  • It's perhabs another hack but it works for me to detect size changes,  In my base Control class, I have Rect bounds fields that maintains bounds information related to it's container canvas.

    public override void SetValue<T>(DependencyProperty property, T obj)
            {
                base.SetValue<T>(property, obj);
                if (property == FrameworkElement.WidthProperty)
                {
                    double width = this.Width;
                    if (this.bounds.Width != width)
                    {
                        this.bounds.Width = width;
                        PerformLayout();
                    }
                }
                else if (property == FrameworkElement.HeightProperty)
                {
                    double height = this.Height;
                    if (this.bounds.Height != height)
                    {
                        this.bounds.Height = height;
                        PerformLayout();
                    }
                }
                else if (property == Canvas.TopProperty)
                {
                    this.bounds.Y = (double)this.GetValue(Canvas.TopProperty);
                }
                else if (property == Canvas.LeftProperty)
                {
                    this.bounds.X = (double)this.GetValue(Canvas.LeftProperty);
                }
            }

            public double Top // and also , Left, Location, Size etc..
            {
                get
                {
                    return this.bounds.Top;
                }
                set
                {
                    if (this.bounds.Top != value)
                    {
                        this.bounds.Y = value;
                        base.SetValue<double>(Canvas.TopProperty, value);
                        OnLocationChanged(this.bounds.Location);
                    }
                }
            }

    Thursday, November 1, 2007 5:44 PM