private void SetExpanded(bool expand) { // Don't do anything if the new state is the same as the previous state if (IsExpanded == expand) { return; } if (expand) { // Transition the visual state to expanded VisualStateManager.GoToState(this, ExpanderStatesVisualStateExpandedName, false); // Find the first focusable element in the expanded content presenter and set focus to it. if (IsFocusFirstChildOnExpandEnabled && !_expandedContentPresenter.SetFocusOnChildControl(FocusState.Programmatic)) { // If we're unable to get the first focusable element, it may be because x:DeferLoadStrategy was used, wait for the content // to load then set focus. var element = _expandedContentPresenter.Content as FrameworkElement; if (element != null) { element.Loaded += OnContentPresenterContentLoaded; } } } else { VisualStateManager.GoToState(this, ExpanderStatesVisualStateCollapsedName, false); } IsExpanded = !IsExpanded; }