/// <summary>
 /// Creates the adorned framework element by searching for a <see cref="DataTemplate"/> that has the same type
 /// as the <see cref="AdornerContent"/>.
 /// </summary>
 private void CreateAdornedFrameworkElementFromDataTemplateInResources()
 {
     foreach (var resourceDictionary in Application.Current.Resources.MergedDictionaries)
     {
         foreach (var resource in resourceDictionary.Values.OfType <DataTemplate>())
         {
             if ((Type)resource.DataType == AdornerContent.GetType())
             {
                 _adornedFrameworkElement = resource.LoadContent() as FrameworkElement;
                 if (_adornedFrameworkElement != null)
                 {
                     _adornedFrameworkElement.DataContext = AdornerContent;
                 }
             }
         }
     }
 }
Beispiel #2
0
            protected override Size ArrangeOverride(Size finalSize)
            {
                if (Owner == null || AdornerContent == null)
                {
                    return(finalSize);
                }
                var element = AdornerContent as FrameworkElement;

                if (element == null)
                {
                    return(finalSize);
                }

                var finalWidth  = finalSize.Width.GetRealOrDefault();
                var finalHeight = finalSize.Height.GetRealOrDefault();

                var desiredWidth = element != null?element.Width.GetRealOrDefault(finalWidth) : finalWidth;

                var calculateWidth = !double.IsNaN(Owner.Left) && !double.IsNaN(Owner.Right);
                var width          = Math.Max(0d, calculateWidth
                    ? finalWidth - Owner.Left - Owner.Right
                    : desiredWidth);

                var desiredHeight = element != null?element.Height.GetRealOrDefault(finalHeight) : finalHeight;

                var calculateHeight = !double.IsNaN(Owner.Top) && !double.IsNaN(Owner.Bottom);
                var height          = Math.Max(0d, calculateHeight
                    ? finalHeight - Owner.Top - Owner.Bottom
                    : desiredHeight);

                var calculateLeft = double.IsNaN(Owner.Left);
                var left          = calculateLeft
                    ? finalWidth - width - Owner.Right.GetRealOrDefault()
                    : Owner.Left.GetRealOrDefault();

                var calculateTop = double.IsNaN(Owner.Left);
                var top          = calculateTop
                    ? finalHeight - height - Owner.Bottom.GetRealOrDefault()
                    : Owner.Top.GetRealOrDefault();

                AdornerContent.Arrange(new Rect(left, top, width, height));
                return(finalSize);
            }