Fixing the Silverlight 1.1 SDK Alpha controls

It’s great that the source code is provided for these controls. I’ve had to fix the ListBox so that it scrolls back to the top when UpdateItems is called, otherwise the canvas can appear empty when drilling down through menus:

        //Updates the content children with the current items in the list
        public void UpdateItems()
        {
            //remove all the children and start from empty
            content.Children.Clear();

            content.SetValue(Canvas.TopProperty, 0);

I’ve also had to fix the list box so that after scrolling it doesn’t select content if you release the mouse button inside the content area which has greatly reduced the number of mis-selections:

        /// <summary>
        /// Keep track of where mouse was originally pressed
        /// </summary>
        private bool mouseDownInsideContentArea;

        // CaptureMouse
        protected void OnMouseLeftButtonDown(object sender, MouseEventArgs args)
        {
            if (CheckMousePosition(args))
            {
                ActualControl.CaptureMouse();
                mouseDownInsideContentArea = true;
            }
            else
            {
                mouseDownInsideContentArea = false;
            }
        }

        // If the mouse is on the ListBox find on which item and select it.
        protected void OnMouseLeftButtonUp(object sender, MouseEventArgs args)
        {
            ActualControl.ReleaseMouseCapture();

            //do thing only if we are outside the ScrollBar [Simon Ransom: and the mouse was originally pressed when not on scroll bar]
            if (CheckMousePosition(args) && mouseDownInsideContentArea) {
                Point pt = args.GetPosition(this);
                if ((items.Count > 0) &&
                    (pt.X < Width) && (pt.Y < Height)) {
                    double contentY = pt.Y - (double)(content.GetValue(Canvas.TopProperty));
                    int itemNumber = (int)(contentY / itemHeight);
                    FrameworkElement newSelection = itemNumber < items.Count ? items[itemNumber] : null;
                    if (selectedItem != newSelection) {
                        Select(newSelection);
                        if (SelectionChanged != null) {
                            SelectionChanged(this, null);
                        }
                    }
                }
            }
        }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s