private bool IsPropertySetLocally(object editor, DependencyProperty dp)
        {
            if (dp == null)
            {
                return(false);
            }

            var editorObject = editor as DependencyObject;

            if (editorObject == null)
            {
                return(false);
            }

            var valueSource = DependencyPropertyHelper.GetValueSource(editorObject, dp);

            if (valueSource == null)
            {
                return(false);
            }

            return(valueSource.BaseValueSource == BaseValueSource.Local);
        }
Example #2
0
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);

            if (element is RibbonGallery)
            {
                HasGallery = (++_galleryCount > 0);
            }
            else
            {
                RibbonSeparator separator = element as RibbonSeparator;
                if (separator != null)
                {
                    ValueSource vs = DependencyPropertyHelper.GetValueSource(separator, StyleProperty);
                    if (vs.BaseValueSource <= BaseValueSource.ImplicitStyleReference)
                    {
                        separator.SetResourceReference(StyleProperty, MenuItem.SeparatorStyleKey);
                    }

                    separator.DefaultStyleKeyInternal = MenuItem.SeparatorStyleKey;
                }
            }
        }
        static void UpdateStyles <TargetColumnClassT>(DataGrid grid, DependencyProperty sourceProperty, DependencyProperty targetProperty) where TargetColumnClassT : DataGridColumn
        {
            var style = grid.GetValue(sourceProperty) as Style;

            foreach (TargetColumnClassT column in grid.Columns.OfType <TargetColumnClassT>())
            {
                var source = DependencyPropertyHelper.GetValueSource(column, targetProperty);
                if (source.BaseValueSource == BaseValueSource.Default)
                {
                    if (style != null)
                    {
                        // override current value
                        column.SetCurrentValue(targetProperty, style);
                    }
                    else
                    {
                        // restore original value
                        var original = column.GetValue(targetProperty);
                        column.SetCurrentValue(targetProperty, original);
                    }
                }
            }
        }
Example #4
0
        protected override Size ArrangeOverride(Size finalSize)
        {
            // Position children.
            foreach (UIElement child in Children)
            {
                // Add translate transform.
                var translate = child.RenderTransform as TranslateTransform;
                if (translate == null)
                {
                    translate             = new TranslateTransform();
                    child.RenderTransform = translate;
                }

                // Get positioning information.
                ValueSource   xSource    = DependencyPropertyHelper.GetValueSource(child, XProperty);
                object        x          = xSource.BaseValueSource == BaseValueSource.Default ? null : child.GetValue(XProperty);
                ValueSource   ySource    = DependencyPropertyHelper.GetValueSource(child, YProperty);
                object        y          = ySource.BaseValueSource == BaseValueSource.Default ? null : child.GetValue(YProperty);
                AxisAlignment alignmentX = (AxisAlignment)child.GetValue(AlignmentXProperty);
                AxisAlignment alignmentY = (AxisAlignment)child.GetValue(AlignmentYProperty);

                // Position.
                if (x != null)
                {
                    translate.X = PositionInInterval(VisibleIntervalX, finalSize.Width, child.DesiredSize.Width, (TX)x, alignmentX);
                }
                if (y != null)
                {
                    translate.Y = PositionInInterval(VisibleIntervalY, finalSize.Height, child.DesiredSize.Height, (TY)y, alignmentY);
                }

                // Arrange.
                child.Arrange(new Rect(new Point(0, 0), child.DesiredSize));
            }

            return(finalSize);
        }
Example #5
0
        protected override Size MeasureOverride(Size availableSize)
        {
            // This panel needs a requested size in order for its children to be able to be positioned.
            if (double.IsInfinity(availableSize.Width) || double.IsInfinity(availableSize.Height))
            {
                return(Size.Empty);
            }

            // Allow children to take up as much room as they want.
            foreach (UIElement child in Children)
            {
                // Verify desired width and height, and set when needed.
                FrameworkElement element = child as FrameworkElement;
                if (element != null)
                {
                    // Resize in case size is specified.
                    ValueSource sizeXSource = DependencyPropertyHelper.GetValueSource(child, SizeXProperty);
                    object      sizeX       = sizeXSource.BaseValueSource == BaseValueSource.Default ? null : child.GetValue(SizeXProperty);
                    ValueSource sizeYSource = DependencyPropertyHelper.GetValueSource(child, SizeYProperty);
                    object      sizeY       = sizeYSource.BaseValueSource == BaseValueSource.Default ? null : child.GetValue(SizeYProperty);
                    if (sizeX != null)
                    {
                        element.Width = IntervalSize(VisibleIntervalX, (TXSize)sizeX, availableSize.Width);
                    }
                    if (sizeY != null)
                    {
                        element.Height = IntervalSize(VisibleIntervalY, (TYSize)sizeY, availableSize.Height);
                    }
                }

                // Child elements are given as much space as they want.
                child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            }

            // The idea behind this panel is to display the specified plane area in the available size.
            return(availableSize);
        }
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);

            if (ItemContainerStyleSelector != null && element is FrameworkElement container)
            {
                var style = ItemContainerStyleSelector.SelectStyle(item, element);
                if (style != null)
                {
                    container.Style = style;
                }

                if (container is ItemsControl itemsControl)
                {
                    itemsControl.SetValueIfDefault(ItemContainerStyleSelectorProperty, ItemContainerStyleSelector);
                }
            }

            if (ItemContainerTemplateSelector != null && element is MenuItem menuItem)
            {
                menuItem.SetValueIfDefault(ItemContainerTemplateSelectorProperty, ItemContainerTemplateSelector);
                menuItem.SetValueIfDefault(UsesItemContainerTemplateProperty, UsesItemContainerTemplate);
            }

            var itemsCommandParameterPropertySource = DependencyPropertyHelper.GetValueSource(this, ItemsCommandParameterProperty);

            if (itemsCommandParameterPropertySource.BaseValueSource > BaseValueSource.Default)
            {
                element.SetBindingIfDefault(MenuItem.CommandParameterProperty,
                                            new Binding
                {
                    Source = this,
                    Path   = new PropertyPath(ItemsCommandParameterProperty)
                });
            }
        }
Example #7
0
 public static bool HasDefaultValue(this DependencyObject d, DependencyProperty dp)
 {
     return(DependencyPropertyHelper.GetValueSource(d, dp).BaseValueSource == BaseValueSource.Default);
 }
        internal void UpdateAdvanceOptionsForItem(MarkupObject markupObject, DependencyObject dependencyObject, DependencyPropertyDescriptor dpDescriptor,
                                                  out object tooltip)
        {
            tooltip = StringConstants.AdvancedProperties;

            bool isResource        = false;
            bool isDynamicResource = false;

            var markupProperty = markupObject.Properties.Where(p => p.Name == PropertyName).FirstOrDefault();

            if ((markupProperty != null) &&
                (markupProperty.PropertyType != typeof(object)) &&
                !markupProperty.PropertyType.IsEnum &&
                !markupProperty.PropertyType.IsArray)
            {
                //TODO: need to find a better way to determine if a StaticResource has been applied to any property not just a style
                isResource        = (markupProperty.Value is Style);
                isDynamicResource = (markupProperty.Value is DynamicResourceExtension);
            }

            if (isResource || isDynamicResource)
            {
                tooltip = StringConstants.Resource;
            }
            else
            {
                if ((dependencyObject != null) && (dpDescriptor != null))
                {
                    if (BindingOperations.GetBindingExpressionBase(dependencyObject, dpDescriptor.DependencyProperty) != null)
                    {
                        tooltip = StringConstants.Databinding;
                    }
                    else
                    {
                        BaseValueSource bvs =
                            DependencyPropertyHelper
                            .GetValueSource(dependencyObject, dpDescriptor.DependencyProperty)
                            .BaseValueSource;

                        switch (bvs)
                        {
                        case BaseValueSource.Inherited:
                        case BaseValueSource.DefaultStyle:
                        case BaseValueSource.ImplicitStyleReference:
                            tooltip = StringConstants.Inheritance;
                            break;

                        case BaseValueSource.DefaultStyleTrigger:
                            break;

                        case BaseValueSource.Style:
                            tooltip = StringConstants.StyleSetter;
                            break;

                        case BaseValueSource.Local:
                            tooltip = StringConstants.Local;
                            break;
                        }
                    }
                }
            }
        }
        protected override async void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (_isLoaded || this.AssociatedObject == null)
            {
                return;
            }

            // we use the property changed thing because the bindings to the attached behaviors are not resolved until after
            // the events (loaded/initialized) have passed.. and this is also the reason we have removed initialized command thing

            if (e.Property == LoadedCommandProperty && this.LoadedCommand != null)
            {
                if (this.LoadedCommandParameter != null || !DependencyPropertyHelper.GetValueSource(this, LoadedCommandParameterProperty).IsExpression)
                {
                    _isLoaded = true;
                    if (this.LoadedCommand.CanExecute(this.LoadedCommandParameter))
                    {
                        this.LoadedCommand.Execute(this.LoadedCommandParameter);
                    }

                    if (this.AssociatedObject != null)
                    {
                        var _viewSupporter = this.AssociatedObject.DataContext as ISupportView;
                        if (_viewSupporter != null)
                        {
                            _viewSupporter.Loaded();
                            await _viewSupporter.LoadedAsync();
                        }
                    }
                }
            }
            else if (e.Property == LoadedCommandParameterProperty && this.LoadedCommandParameter != null)
            {
                if (this.LoadedCommand != null)
                {
                    _isLoaded = true;
                    if (this.LoadedCommand.CanExecute(this.LoadedCommandParameter))
                    {
                        this.LoadedCommand.Execute(this.LoadedCommandParameter);
                    }

                    if (this.AssociatedObject != null)
                    {
                        var _viewSupporter = this.AssociatedObject.DataContext as ISupportView;
                        if (_viewSupporter != null)
                        {
                            _viewSupporter.Loaded();
                            await _viewSupporter.LoadedAsync();
                        }
                    }
                }
            }
            else if (this.LoadedCommand == null && !DependencyPropertyHelper.GetValueSource(this, LoadedCommandProperty).IsExpression)
            {
                _isLoaded = true;
                if (this.AssociatedObject != null)
                {
                    var _viewSupporter = this.AssociatedObject.DataContext as ISupportView;
                    if (_viewSupporter != null)
                    {
                        _viewSupporter.Loaded();
                        await _viewSupporter.LoadedAsync();
                    }
                }
            }

            //if (e.Property == InitializedCommandProperty && this.InitializedCommand != null)
            //{
            //    if (this.InitializedCommand != null || !DependencyPropertyHelper.GetValueSource(this, InitializedCommandParameterProperty).IsExpression)
            //    {
            //        if (this.InitializedCommand.CanExecute(this.InitializedCommandParameter))
            //            this.InitializedCommand.Execute(this.InitializedCommandParameter);
            //    }
            //}
            //else if (e.Property == InitializedCommandParameterProperty && this.InitializedCommandParameter != null)
            //{
            //    if (this.InitializedCommand != null)
            //    {
            //        if (this.InitializedCommand.CanExecute(this.InitializedCommandParameter))
            //            this.InitializedCommand.Execute(this.InitializedCommandParameter);
            //    }
            //}
        }
        private void Update()
        {
            if (this.ignoreUpdate)
            {
                return;
            }

            this.isLocallySet     = false;
            this.isInvalidBinding = false;
            this.isDatabound      = false;

            var dp = this.DependencyProperty;
            var d  = this.Target as DependencyObject;

            if (SnoopModes.MultipleDispatcherMode &&
                d != null &&
                d.Dispatcher != this.Dispatcher)
            {
                return;
            }

            if (dp != null &&
                d != null)
            {
                //Debugger.Launch();
                if (d.ReadLocalValue(dp) != DependencyProperty.UnsetValue)
                {
                    this.isLocallySet = true;
                }

                var expression = BindingOperations.GetBindingExpressionBase(d, dp);
                if (expression != null)
                {
                    this.isDatabound = true;

                    if (expression.HasError ||
                        (expression.Status != BindingStatus.Active && !(expression is PriorityBindingExpression)))
                    {
                        this.isInvalidBinding = true;

                        var builder = new StringBuilder();
                        var writer  = new StringWriter(builder);
                        var tracer  = new TextWriterTraceListener(writer);
                        PresentationTraceSources.DataBindingSource.Listeners.Add(tracer);

                        // reset binding to get the error message.
                        this.ignoreUpdate = true;
                        d.ClearValue(dp);
                        BindingOperations.SetBinding(d, dp, expression.ParentBindingBase);
                        this.ignoreUpdate = false;

                        // this needs to happen on idle so that we can actually run the binding, which may occur asynchronously.
                        this.RunInDispatcherAsync(
                            () =>
                        {
                            this.bindingError = builder.ToString();
                            this.OnPropertyChanged(nameof(this.BindingError));
                            PresentationTraceSources.DataBindingSource.Listeners.Remove(tracer);
                            writer.Close();
                        }, DispatcherPriority.ApplicationIdle);
                    }
                    else
                    {
                        this.bindingError = string.Empty;
                    }
                }

                this.valueSource = DependencyPropertyHelper.GetValueSource(d, dp);
            }

            this.OnPropertyChanged(nameof(this.IsLocallySet));
            this.OnPropertyChanged(nameof(this.IsInvalidBinding));
            this.OnPropertyChanged(nameof(this.StringValue));
            this.OnPropertyChanged(nameof(this.ResourceKey));
            this.OnPropertyChanged(nameof(this.DescriptiveValue));
            this.OnPropertyChanged(nameof(this.IsDatabound));
            this.OnPropertyChanged(nameof(this.IsExpression));
            this.OnPropertyChanged(nameof(this.IsAnimated));
            this.OnPropertyChanged(nameof(this.ValueSource));
        }
Example #11
0
        /// <summary>
        ///   Computes the text alignment of the adorned element.
        /// </summary>
        /// <returns>
        ///   Returns the computed text alignment.
        /// </returns>
        private TextAlignment ComputedTextAlignment()
        {
            var adornedElement = AdornedElement as Control;

            var textBox = adornedElement as TextBox;

            if (textBox != null)
            {
                if (DependencyPropertyHelper.GetValueSource(textBox, TextBox.HorizontalContentAlignmentProperty)
                    .BaseValueSource != BaseValueSource.Local || DependencyPropertyHelper.GetValueSource(textBox, TextBox.TextAlignmentProperty)
                    .BaseValueSource == BaseValueSource.Local)
                {
                    // TextAlignment dominates
                    return(textBox.TextAlignment);
                }
            }

            var richTextBox = adornedElement as RichTextBox;

            if (richTextBox != null)
            {
                var blocks = richTextBox.Document.Blocks;

                var textAlignment = richTextBox.Document.TextAlignment;

                if (blocks.Count == 0)
                {
                    return(textAlignment);
                }

                if (blocks.Count == 1)
                {
                    var paragraph = blocks.FirstBlock as Paragraph;
                    if (paragraph == null)
                    {
                        return(textAlignment);
                    }

                    return(paragraph.TextAlignment);
                }

                return(textAlignment);
            }

            if (adornedElement != null)
            {
                switch (adornedElement.HorizontalContentAlignment)
                {
                case HorizontalAlignment.Left:
                    return(TextAlignment.Left);

                case HorizontalAlignment.Right:
                    return(TextAlignment.Right);

                case HorizontalAlignment.Center:
                    return(TextAlignment.Center);

                case HorizontalAlignment.Stretch:
                    return(TextAlignment.Justify);
                }
            }

            return(TextAlignment.Left);
        }
Example #12
0
 public static bool NotSet(this DependencyObject depObj, DependencyProperty prop)
 {
     return(DependencyPropertyHelper.GetValueSource(depObj, prop).BaseValueSource != BaseValueSource.Local);
 }
Example #13
0
        public static bool IsPropertyDefault(DependencyObject obj, DependencyProperty dp)
        {
            BaseValueSource baseValueSource = DependencyPropertyHelper.GetValueSource(obj, dp).BaseValueSource;

            return(baseValueSource == BaseValueSource.Default);
        }
Example #14
0
 /// <summary>
 /// 判断某个属性当前的值是否为本地值。
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="property"></param>
 /// <returns></returns>
 public static bool IsLocalValue(this DependencyObject obj, DependencyProperty property)
 {
     return(DependencyPropertyHelper.GetValueSource(obj, property).BaseValueSource == BaseValueSource.Local);
 }
 static void propertyCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     ((AwesomeTextbox)d).TheRectangle.Fill = ((AwesomeTextbox)d).SquareColor;
     DependencyPropertyHelper.GetValueSource(d, SquareColorProperty);
 }
Example #16
0
 public static bool IsDefaultValue(DependencyObject d, DependencyProperty dp)
 => DependencyPropertyHelper.GetValueSource(d, dp).BaseValueSource == BaseValueSource.Default;
Example #17
0
 /// <summary>
 /// Taken from Helper code for DataGrid.
 /// </summary>
 private static bool IsDefaultValue(DependencyObject d, DependencyProperty dp)
 {
     return(DependencyPropertyHelper.GetValueSource(d, dp).BaseValueSource == BaseValueSource.Default);
 }
        internal void UpdateAdvanceOptionsForItem(MarkupObject markupObject, DependencyObject dependencyObject, DependencyPropertyDescriptor dpDescriptor,
                                                  out string imageName, out object tooltip)
        {
            imageName = "AdvancedProperties11";
            tooltip   = "Advanced Properties";

            bool isResource        = false;
            bool isDynamicResource = false;

            var markupProperty = markupObject.Properties.Where(p => p.Name == PropertyName).FirstOrDefault();

            if (markupProperty != null)
            {
                //TODO: need to find a better way to determine if a StaticResource has been applied to any property not just a style
                isResource        = (markupProperty.Value is Style);
                isDynamicResource = (markupProperty.Value is DynamicResourceExtension);
            }

            if (isResource || isDynamicResource)
            {
                imageName = "Resource11";
                tooltip   = "Resource";
            }
            else
            {
                if ((dependencyObject != null) && (dpDescriptor != null))
                {
                    if (BindingOperations.GetBindingExpressionBase(dependencyObject, dpDescriptor.DependencyProperty) != null)
                    {
                        imageName = "Database11";
                        tooltip   = "Databinding";
                    }
                    else
                    {
                        BaseValueSource bvs =
                            DependencyPropertyHelper
                            .GetValueSource(dependencyObject, dpDescriptor.DependencyProperty)
                            .BaseValueSource;

                        switch (bvs)
                        {
                        case BaseValueSource.Inherited:
                        case BaseValueSource.DefaultStyle:
                        case BaseValueSource.ImplicitStyleReference:
                            imageName = "Inheritance11";
                            tooltip   = "Inheritance";
                            break;

                        case BaseValueSource.DefaultStyleTrigger:
                            break;

                        case BaseValueSource.Style:
                            imageName = "Style11";
                            tooltip   = "Style Setter";
                            break;

                        case BaseValueSource.Local:
                            imageName = "Local11";
                            tooltip   = "Local";
                            break;
                        }
                    }
                }
            }
        }
Example #19
0
        internal void UpdateAdvanceOptionsForItem(DependencyObject dependencyObject, DependencyPropertyDescriptor dpDescriptor, out object tooltip)
        {
            tooltip = StringConstants.Default;

            bool isResource        = false;
            bool isDynamicResource = false;

            //TODO: need to find a better way to determine if a StaticResource has been applied to any property not just a style(maybe with StaticResourceExtension)
            isResource        = typeof(Style).IsAssignableFrom(this.PropertyType);
            isDynamicResource = typeof(DynamicResourceExtension).IsAssignableFrom(this.PropertyType);

            if (isResource || isDynamicResource)
            {
                tooltip = StringConstants.Resource;
            }
            else
            {
                if ((dependencyObject != null) && (dpDescriptor != null))
                {
                    if (BindingOperations.GetBindingExpressionBase(dependencyObject, dpDescriptor.DependencyProperty) != null)
                    {
                        tooltip = StringConstants.Databinding;
                    }
                    else
                    {
                        BaseValueSource bvs =
                            DependencyPropertyHelper
                            .GetValueSource(dependencyObject, dpDescriptor.DependencyProperty)
                            .BaseValueSource;

                        switch (bvs)
                        {
                        case BaseValueSource.Inherited:
                        case BaseValueSource.DefaultStyle:
                        case BaseValueSource.ImplicitStyleReference:
                            tooltip = StringConstants.Inheritance;
                            break;

                        case BaseValueSource.DefaultStyleTrigger:
                            break;

                        case BaseValueSource.Style:
                            tooltip = StringConstants.StyleSetter;
                            break;

                        case BaseValueSource.Local:
                            tooltip = StringConstants.Local;
                            break;
                        }
                    }
                }
                else
                {
                    // When the Value is diferent from the DefaultValue, use the local icon.
                    if (!object.Equals(this.Value, this.DefaultValue))
                    {
                        if (this.DefaultValue != null)
                        {
                            tooltip = StringConstants.Local;
                        }
                        else
                        {
                            if (this.PropertyType.IsValueType)
                            {
                                var defaultValue = Activator.CreateInstance(this.PropertyType);
                                // When the Value is diferent from the DefaultValue, use the local icon.
                                if (!object.Equals(this.Value, defaultValue))
                                {
                                    tooltip = StringConstants.Local;
                                }
                            }
                            else
                            {
                                // When the Value is diferent from null, use the local icon.
                                if (this.Value != null)
                                {
                                    tooltip = StringConstants.Local;
                                }
                            }
                        }
                    }
                }
            }
        }
Example #20
0
        private void Update()
        {
            if (ignoreUpdate)
            {
                return;
            }

            this.isLocallySet     = false;
            this.isInvalidBinding = false;
            this.isDatabound      = false;

            DependencyProperty dp = this.DependencyProperty;
            DependencyObject   d  = this.Target as DependencyObject;

            if (SnoopModes.MultipleDispatcherMode && d != null && d.Dispatcher != this.Dispatcher)
            {
                return;
            }

            if (dp != null && d != null)
            {
                if (d.ReadLocalValue(dp) != DependencyProperty.UnsetValue)
                {
                    this.isLocallySet = true;
                }

                BindingExpressionBase expression = BindingOperations.GetBindingExpressionBase(d, dp);
                if (expression != null)
                {
                    this.isDatabound = true;

                    if (expression.HasError || expression.Status != BindingStatus.Active && !(expression is PriorityBindingExpression))
                    {
                        this.isInvalidBinding = true;

                        StringBuilder           builder = new StringBuilder();
                        StringWriter            writer  = new StringWriter(builder);
                        TextWriterTraceListener tracer  = new TextWriterTraceListener(writer);
                        PresentationTraceSources.DataBindingSource.Listeners.Add(tracer);

                        // reset binding to get the error message.
                        ignoreUpdate = true;
                        d.ClearValue(dp);
                        BindingOperations.SetBinding(d, dp, expression.ParentBindingBase);
                        ignoreUpdate = false;

                        // cplotts note: maciek ... are you saying that this is another, more concise way to dispatch the following code?
                        //Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, () =>
                        //    {
                        //        bindingError = builder.ToString();
                        //        this.OnPropertyChanged("BindingError");
                        //        PresentationTraceSources.DataBindingSource.Listeners.Remove(tracer);
                        //        writer.Close();
                        //    });

                        // this needs to happen on idle so that we can actually run the binding, which may occur asynchronously.
                        Dispatcher.BeginInvoke
                        (
                            DispatcherPriority.ApplicationIdle,
                            new DispatcherOperationCallback
                            (
                                delegate(object source)
                        {
                            bindingError = builder.ToString();
                            this.OnPropertyChanged("BindingError");
                            PresentationTraceSources.DataBindingSource.Listeners.Remove(tracer);
                            writer.Close();
                            return(null);
                        }
                            ),
                            null
                        );
                    }
                    else
                    {
                        bindingError = string.Empty;
                    }
                }

                this.valueSource = DependencyPropertyHelper.GetValueSource(d, dp);
            }

            this.OnPropertyChanged("IsLocallySet");
            this.OnPropertyChanged("IsInvalidBinding");
            this.OnPropertyChanged("StringValue");
            this.OnPropertyChanged("DescriptiveValue");
            this.OnPropertyChanged("IsDatabound");
            this.OnPropertyChanged("IsExpression");
            this.OnPropertyChanged("IsAnimated");
            this.OnPropertyChanged("ValueSource");
        }