public void Arrange(UIElement element, Rectangle availableSize) { var bounds = new Rectangle((int)element.Position.X, (int)element.Position.Y, (int)element.Width, (int)element.Height); // Set the X position switch (element.HorizontalAlignment) { case HorizontalAlignment.Left: element.Position.X = availableSize.Left; break; case HorizontalAlignment.Stretch: case HorizontalAlignment.Center: element.Position.X = availableSize.Center.X - (bounds.Width / 2); break; case HorizontalAlignment.Right: element.Position.X = availableSize.Right - bounds.Width; break; } // Set the Y position switch (element.VerticalAlignment) { case VerticalAlignment.Top: element.Position.Y = availableSize.Top; break; case VerticalAlignment.Stretch: case VerticalAlignment.Center: element.Position.Y = availableSize.Center.Y - (bounds.Height / 2); break; case VerticalAlignment.Bottom: element.Position.Y = availableSize.Bottom - bounds.Height; break; } // Apply margins element.Position.X += element.Margin.Left; element.Position.X -= element.Margin.Right; element.Position.Y += element.Margin.Top; element.Position.Y -= element.Margin.Bottom; }
public void Arrange(UIElement element) { Arrange(element, availableSize); }
private void HandleMouseHover() { var element = GetElementAffectedByMouseEvent(Elements); if (element != null) { if (currentlyHighlightedElement != element) { element.IsHighlighted = true; if (currentlyHighlightedElement != null) { currentlyHighlightedElement.IsHighlighted = false; } currentlyHighlightedElement = element; // Fire the state-change event if (ElementStateChanged != null) { ElementStateChanged(element, new ElementStateChangeArgs() { State = ElementState.Highlighted }); } } } }
private void HandleLeftMouseButtonPressed(MouseState mouseState) { if (inputContext.Mouse.MouseState[MouseInputType.LeftButton].WasJustPressed) { var element = GetElementAffectedByMouseEvent(Elements); if (element != null) { if (currentlyFocusedElement != element) { element.IsFocused = true; if (currentlyFocusedElement != null) { currentlyFocusedElement.IsFocused = false; } currentlyFocusedElement = element; } element.HandleMouseEvent(mouseState); // Fire the state-change event if (ElementStateChanged != null) { ElementStateChanged(element, new ElementStateChangeArgs() { State = ElementState.Focused }); } } } }
private UIElement GetChildElementAffectedByMouseEvent(UIElement element) { if (element is Panel) { return GetElementAffectedByMouseEvent((element as Panel).Children); } else if (element is StackPanel) { return GetElementAffectedByMouseEvent((element as StackPanel).Children); } return element; }
public void SetFocus(UIElement element) { ClearFocus(); element.SetFocus(true); currentlyFocusedElement = element; currentlyHighlightedElement = null; }
public void ClearFocus() { if (currentlyFocusedElement != null) { currentlyFocusedElement.SetFocus(false); } currentlyFocusedElement = null; }