views:

63

answers:

1

Good Day,

I have a Silverlight control that appears on top of another (picture a pop-up box). In this pop-up control, I have a scrollview (height = 250) with a stack panel (instance name = spMain, orientation = vertical) inside. Within the contents of the stack panel are several textboxes stacked on top of each other. When I tab from textbox to textbox, the scrollviewer automatically moves toward the bottom (I wrote code in an event handler that all textboxes are linked to that does that).

The problem I'm having is when I attempt to close out the pop-up control, I'm receiving an error stating that the value does not fall within the expected range.

            if (sender is TextBox)
            {
                TextBox tb = (TextBox)sender;

                try
                {
                    // Code bombs out here when I attempt to close out the pop-up control
                    Point pt = tb.TransformToVisual(spMain).Transform(new Point());

                    if (pt.Y >= scrollViewerHeight - tb.ActualHeight)
                    {
                        svMain.UpdateLayout();
                        svMain.ScrollToVerticalOffset(scrollViewerHeight += pt.Y);
                    }
                }
                catch (ArgumentException aex)
                {
                    // Don't want to eat the exception
                    string errorMessage = aex.Message;
                    System.Diagnostics.Debug.WriteLine(errorMessage);
                }
            }

I'm not surprised I'm getting the error, because it appears to make sense, but what I'm looking for is some sort of User Control Unloaded event or prevent the offending code from executing.

Does anyone have any ideas on how to go about this?

TIA,

coson

A: 

I think the problem lies with how you're closing the popup. Are you removing it from the visual tree or just setting its visibility to collapsed?

If (as I think your problem suggests) you're removing it from the visual tree entirely, you may be able to solve the immediate problem by collapsing the visibility of your text boxes first, and then removing the control. That assumes that your code above is being called as a result of a resize (or a potential resize) of the textboxes. Alternately, you could just empty them of content before you remove them also.

As for an Unloaded event, there isn't anything in the framework that'll do that for you. You could write a custom unload method for your control easily enough though, and just use that when you want to remove it.

Raumornie