public void Should_Work()
        {
            int start = 0;
            int end   = 20;

            Assert.Equal(Enumerable.Range(start, end - start), RecursiveEnumerable.Get(start, i => i += 1, end));
        }
Example #2
0
 private static void OnInlinesChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
 {
     if (obj is TextBlock textBlock)
     {
         textBlock.Inlines.Clear();
         if (e.NewValue is IEnumerable <InlineModel> inlines)
         {
             ICollectionExtensions.AddRange(textBlock.Inlines, inlines.Select(i => i.Create(textBlock)));
         }
     }
     else if (obj is Paragraph paragraph)
     {
         paragraph.Inlines.Clear();
         if (e.NewValue is IEnumerable <InlineModel> inlines &&
             RecursiveEnumerable.While(paragraph.Parent, obj => obj is FrameworkContentElement fce, obj => ((FrameworkContentElement)obj).Parent).LastOrDefault() is FrameworkElement fe)
         {
             ICollectionExtensions.AddRange(paragraph.Inlines, inlines.Select(i => i.Create(fe)));
         }
     }
 }
Example #3
0
        public object ProvideValue(object targetObject, object targetProperty, IServiceProvider?serviceProvider)
        {
            if (targetObject is Setter setter)
            {
                var resourceKey = GetResourceKey(setter.Property);
                if (Palette.IsDynamicResourceKey(resourceKey))
                {
                    Debug.WriteLineIf(Converter != null, $"Warning: {nameof(Converter)} set on {nameof(FromPalette)} but when {nameof(targetObject)} is not a {nameof(FrameworkElement)} and has no Static ${nameof(Overload)} the {nameof(Converter)} is ignored");
                    return(new DynamicResourceExtension(resourceKey));
                }
                else //static
                {
                    return(ApplyConverter(Application.Current.FindResource(resourceKey), setter.Property?.PropertyType ?? FallbackPropertyType));
                }
            }
            else if (targetObject is DiscreteObjectKeyFrame)
            {
                var resourceKey = GetResourceKey(null);
                return(ApplyConverter(Application.Current.FindResource(resourceKey), FallbackPropertyType));
            }
            else if (targetObject is ColorAnimationUsingKeyFrames)
            {
                FallbackPropertyType = typeof(Color);
                var resourceKey = GetResourceKey(null);
                return(ApplyConverter(Application.Current.FindResource(resourceKey), FallbackPropertyType));
            }
            else if (targetProperty is DependencyProperty dp)
            {
                var element = targetObject as FrameworkElement;

                var resourceKey = GetResourceKey(dp);
                if (element != null)
                {
                    return(ApplyConverter(element.FindResource(resourceKey), dp.PropertyType));
                }
                else if (targetObject is FrameworkContentElement fce)
                {
                    //Walk up the path to find a framework element
                    var parentTree = RecursiveEnumerable.Get((object?)fce, fce => (fce as FrameworkContentElement)?.Parent, null);
                    element = parentTree.FirstOrDefault(i => i is FrameworkElement) as FrameworkElement;
                    if (element != null)
                    {
                        return(ApplyConverter(element.FindResource(resourceKey), dp.PropertyType));
                    }
                    else if (!Palette.IsDynamicResourceKey(resourceKey))
                    {
                        return(ApplyConverter(Application.Current.FindResource(resourceKey), dp.PropertyType));
                    }
                    else
                    {
                        throw new NotSupportedException($"For a {nameof(FrameworkContentElement)} target there must be a {nameof(FrameworkElement)} in its parent tree. Or it should not depend on an active {nameof(Palette)}");
                    }
                }
                else if (Palette.IsDynamicResourceKey(resourceKey) && serviceProvider != null)
                {
                    Debug.WriteLineIf(Converter != null, $"Warning: {nameof(Converter)} set on {nameof(FromPalette)} but when {nameof(targetObject)} is not a {nameof(FrameworkElement)} and has no Static ${nameof(Overload)} the {nameof(Converter)} is ignored");
                    return(new DynamicResourceExtension(resourceKey).ProvideValue(serviceProvider));
                }
                else //static
                {
                    return(ApplyConverter(Application.Current.FindResource(resourceKey), dp.PropertyType));
                }
            }
            else
            {
                var resourceKey = GetResourceKey(null);
                return(ApplyConverter(Application.Current.FindResource(resourceKey), FallbackPropertyType));
            }
        }
 public Enumerator(RecursiveEnumerable <T> owner)
 {
     _owner = owner;
     Reset();
 }