/// <summary> /// ContentTargetSizeProperty property changed handler. /// </summary> /// <param name="d">AccordionItem that changed its ContentTargetSize.</param> /// <param name="e">Event arguments.</param> private static void OnContentTargetSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var source = (AccordionItem)d; var targetSize = (Size)e.NewValue; if (!source._allowedToWriteContentTargetSize) { // revert to old value source.ContentTargetSize = (Size)e.OldValue; throw new InvalidOperationException("Invalid content and size."); } // Pass the value to the expandSite // This is done explicitly so an animation action can be scheduled // deterministicly. ExpandableContentControl expandSite = source.ExpandSite; if (expandSite != null && !expandSite.TargetSize.Equals(targetSize)) { expandSite.TargetSize = targetSize; if (source.IsSelected) { if (source.ParentAccordion != null && source.ParentAccordion.IsResizing) { // if the accordion is resizing, this item should snap immediately #if SILVERLIGHT expandSite.Percentage = 1; #else expandSite.RecalculatePercentage(1); #endif } else { // otherwise schedule the resize source.Schedule(AccordionAction.Resize); } } } }
/// <summary> /// Recalculates the percentage based on a new size. /// </summary> /// <param name="expandableContentControl">The control which is going to be evaluated</param> /// <param name="value">The new size used to base percentages on.</param> /// <returns>The new percentage value</returns> private static double CalculatePercentage(ExpandableContentControl expandableContentControl, Size value) { double newPercentage = 0.0; if (expandableContentControl.ContentSite != null) { if (expandableContentControl.IsVerticalRevealMode) { newPercentage = expandableContentControl.ActualHeight / (double.IsNaN(value.Height) ? expandableContentControl.ContentSite.DesiredSize.Height : value.Height); } else if (expandableContentControl.IsHorizontalRevealMode) { newPercentage = expandableContentControl.ActualWidth / (double.IsNaN(value.Width) ? expandableContentControl.ContentSite.DesiredSize.Width : value.Width); } } return(newPercentage); }