예제 #1
0
        /// <summary>
        /// If you use a bindable flow document element more than once, you may encounter a "Collection was modified" exception.
        /// The error occurs when the binding is updated because of a change to an inherited dependency property. The most common scenario
        /// is when the inherited DataContext changes. It appears that an inherited properly like DataContext is propagated to its descendants.
        /// When the enumeration of descendants gets to a BindableXXX, the dependency properties of that element change according to the new
        /// DataContext, which change the (non-dependency) properties. However, for some reason, changing the flow content invalidates the
        /// enumeration and raises an exception.
        /// To work around this, one can either DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=FrameworkElement}}"
        /// in code. This is clumsy, so every derived type calls this function instead (which performs the same thing).
        /// See http://code.logos.com/blog/2008/01/data_binding_in_a_flowdocument.html
        /// </summary>
        /// <param name="element"></param>
        public static void FixupDataContext(FrameworkContentElement element)
        {
            Binding b = new Binding(FrameworkContentElement.DataContextProperty.Name);

            // another approach (if this one has problems) is to bind to an ancestor by ElementName
            b.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1);
            element.SetBinding(FrameworkContentElement.DataContextProperty, b);
        }
예제 #2
0
        public static void FixupDataContext(FrameworkContentElement element)
        {
            var binding = new Binding(FrameworkContentElement.DataContextProperty.Name)
            {
                RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(FrameworkElement), 1)
            };

            element.SetBinding(FrameworkContentElement.DataContextProperty, binding);
        }
예제 #3
0
        private static void SetForeBrushBinding([NotNull] FrameworkContentElement inline, [NotNull] object property)
        {
            var binding = new Binding
            {
                RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(TextBlock), 1),
                Path           = new PropertyPath(property)
            };

            inline.SetBinding(TextElement.ForegroundProperty, binding);
        }
예제 #4
0
 public static FrameworkContentElement Bind <T>(this FrameworkContentElement _element, DependencyProperty _dependencyProperty, Expression <Func <T> > _expression, bool _isReadOnly, IValueConverter _converter)
 {
     if (_expression == null)
     {
         _element.SetValue(_dependencyProperty, default(T));
     }
     else
     {
         _element.SetBinding(_dependencyProperty, _expression.GetBinding(_isReadOnly, _converter));
     }
     return(_element);
 }
예제 #5
0
        public static void bind(this FrameworkContentElement bound, DependencyProperty boundProp, object source, string sourceProp,
                                IValueConverter converter = null, object parameter = null)
        {
            Binding b = new Binding {
                Source = source, Path = new PropertyPath(sourceProp)
            };

            if (converter != null)
            {
                b.Converter          = converter;
                b.ConverterParameter = parameter;
            }
            bound.SetBinding(boundProp, b);
        }