Пример #1
0
 public static void SetBindStyle(DependencyObject obj, TextualStyle value)
 {
     obj.Set(BindStyleProperty, value);
 }
Пример #2
0
        //private static void onForegroundMaterialSetRelativeSourceChanged(DependencyObject @this, DependencyPropertyChangedEventArgs e)
        //{
        //	var control = @this as FrameworkElement;
        //	if (control == null)
        //		throw new InvalidCastException("ForegroundMaterialSetRelativeSource target object must be a FrameworkElement.");

        //	var relativeSource = e.NewValue as DependencyObject;
        //	if (relativeSource == null)
        //		throw new InvalidCastException("ForegroundMaterialSetRelativeSource value must be a DependencyObject.");

        //	var bindingRelativeSource = GetBindingRelativeSource(@this);
        //	var textualStyle = GetBindStyle(@this);
        //	var isHighContrastBinding = GetIsHighContrastBinding(@this);
        //	var highContrastBindingContext = GetHighContrastBindingContext(@this);

        //	bindTextualStyleImpl(control, textualStyle, relativeSource, foregroundMaterialSetRelativeSource, isHighContrastBinding, highContrastBindingContext);
        //}
        //private static void onBindStyleChanged(DependencyObject @this, DependencyPropertyChangedEventArgs e)
        //{
        //	var control = @this as FrameworkElement;
        //	if (control == null)
        //		throw new InvalidCastException("BindStyle target object must be a FrameworkElement.");

        //	var textualStyle = e.NewValue as TextualStyle;
        //	if (textualStyle == null)
        //		throw new InvalidCastException("BindStyle value must be a TextualStyle.");

        //	var relativeSource = GetBindingRelativeSource(@this);
        //	var foregroundMaterialSetRelativeSource = GetForegroundMaterialSetRelativeSource(@this);
        //	var isHighContrastBinding = GetIsHighContrastBinding(@this);
        //	var highContrastBindingContext = GetHighContrastBindingContext(@this);

        //	bindTextualStyleImpl(control, textualStyle, relativeSource, foregroundMaterialSetRelativeSource, isHighContrastBinding, highContrastBindingContext);
        //}

        //private static void onIsHighContrastBindingChanged(DependencyObject @this, DependencyPropertyChangedEventArgs e)
        //{
        //	var control = @this as FrameworkElement;
        //	if (control == null)
        //		throw new InvalidCastException("BindStyle target object must be a FrameworkElement.");

        //	var textualStyle = e.NewValue as TextualStyle;
        //	if (textualStyle == null)
        //		throw new InvalidCastException("BindStyle value must be a TextualStyle.");

        //	var relativeSource = GetBindingRelativeSource(@this);
        //	var foregroundMaterialSetRelativeSource = GetForegroundMaterialSetRelativeSource(@this);
        //	var isHighContrastBinding = GetIsHighContrastBinding(@this);
        //	var highContrastBindingContext = GetHighContrastBindingContext(@this);

        //	bindTextualStyleImpl(control, textualStyle, relativeSource, foregroundMaterialSetRelativeSource, isHighContrastBinding, highContrastBindingContext);
        //}

        //private static void onHighContrastBindingContextChanged(DependencyObject @this, DependencyPropertyChangedEventArgs e)
        //{
        //	var control = @this as FrameworkElement;
        //	if (control == null)
        //		throw new InvalidCastException("BindStyle target object must be a FrameworkElement.");

        //	var textualStyle = e.NewValue as TextualStyle;
        //	if (textualStyle == null)
        //		throw new InvalidCastException("BindStyle value must be a TextualStyle.");

        //	var relativeSource = GetBindingRelativeSource(@this);
        //	var foregroundMaterialSetRelativeSource = GetForegroundMaterialSetRelativeSource(@this);
        //	var isHighContrastBinding = GetIsHighContrastBinding(@this);
        //	var highContrastBindingContext = GetHighContrastBindingContext(@this);

        //	bindTextualStyleImpl(control, textualStyle, relativeSource, foregroundMaterialSetRelativeSource, isHighContrastBinding, highContrastBindingContext);
        //}


        private static void bindTextualStyleImpl(
            FrameworkElement @this,
            TextualStyle textualStyle,
            RelativeSource relativeSource,
            DependencyObject foregroundMaterialSetRelativeSource,
            bool isHighContrastBinding,
            SolidColorBrush highContrastBindingContext)
        //double highContrastBindingThreshold)
        {
            BindingOperations.SetBinding(@this, TextElement.FontFamilyProperty, new Binding(FontFamilyProperty.Name)
            {
                Source = textualStyle
            });
            BindingOperations.SetBinding(@this, TextElement.FontStyleProperty, new Binding(FontStyleProperty.Name)
            {
                Source = textualStyle
            });
            BindingOperations.SetBinding(@this, TextElement.FontWeightProperty, new Binding(FontWeightProperty.Name)
            {
                Source = textualStyle
            });
            BindingOperations.SetBinding(@this, TextElement.FontStretchProperty, new Binding(FontStretchProperty.Name)
            {
                Source = textualStyle
            });
            //BindingOperations.SetBinding(@this, Control.PaddingProperty, new Binding(PaddingProperty.Name) { Source = textualStyle });
            BindingOperations.SetBinding(@this, TextElement.FontSizeProperty, new MultiBinding
            {
                Converter = new MultiplyMultiConverter(),
                Bindings  =
                {
                    new Binding
                    {
                        RelativeSource = relativeSource,
                        Path           = new PropertyPath("FontSize")
                    },
                    new Binding
                    {
                        Source = textualStyle,
                        Path   = new PropertyPath("RelativeFontSize")
                    },
                }
            });
            if (foregroundMaterialSetRelativeSource == null)
            {
                if (isHighContrastBinding)
                {
                    BindingOperations.SetBinding(@this, TextElement.ForegroundProperty,
                                                 new MultiBinding
                    {
                        Converter = new HighContrastDescriptorToBrushConverter(),
                        Bindings  =
                        {
                            new Binding
                            {
                                RelativeSource = relativeSource,
                                Path           = new PropertyPath("FallbackMaterialSet")
                            },
                            new Binding
                            {
                                Source = textualStyle,
                                Path   = new PropertyPath("ForegroundDescriptor")
                            },
                            new Binding
                            {
                                RelativeSource = relativeSource,
                                Path           = new PropertyPath("IsHighContrastOverlayedTextEnabled")
                            },
                            new Binding
                            {
                                Source = highContrastBindingContext
                            },
                            new Binding
                            {
                                RelativeSource = relativeSource,
                                Path           = new PropertyPath("HighContrastBindingThreshold")
                            }
                        }
                    });
                }
                else
                {
                    BindingOperations.SetBinding(@this, TextElement.ForegroundProperty,
                                                 new MultiBinding
                    {
                        Converter = new DescriptorToBrushConverter(),
                        Bindings  =
                        {
                            new Binding
                            {
                                RelativeSource = relativeSource,
                                Path           = new PropertyPath("FallbackMaterialSet")
                            },
                            new Binding
                            {
                                Source = textualStyle,
                                Path   = new PropertyPath("ForegroundDescriptor")
                            }
                        }
                    });
                }
            }
            else
            {
                if (isHighContrastBinding)
                {
                    BindingOperations.SetBinding(@this, TextElement.ForegroundProperty,
                                                 new MultiBinding
                    {
                        Converter = new HighContrastDescriptorToBrushConverter(),//MarkupSingleton.GetInstance<>(),
                        Bindings  =
                        {
                            new Binding
                            {
                                Source = foregroundMaterialSetRelativeSource
                            },
                            new Binding
                            {
                                Source = textualStyle,
                                Path   = new PropertyPath("ForegroundDescriptor")
                            },
                            new Binding
                            {
                                RelativeSource = relativeSource,
                                Path           = new PropertyPath("IsHighContrastOverlayedTextEnabled")
                            },
                            new Binding
                            {
                                Source = highContrastBindingContext
                            },
                            new Binding
                            {
                                RelativeSource = relativeSource,
                                Path           = new PropertyPath("HighContrastBindingThreshold")
                            }
                        }
                    });
                }
                else
                {
                    BindingOperations.SetBinding(@this, TextElement.ForegroundProperty,
                                                 new MultiBinding
                    {
                        Converter = new DescriptorToBrushConverter(), //TODO use getinstance<> for all these, stop creating instances
                        Bindings  =
                        {
                            new Binding
                            {
                                Source = foregroundMaterialSetRelativeSource
                            },
                            new Binding
                            {
                                Source = textualStyle,
                                Path   = new PropertyPath("ForegroundDescriptor")
                            }
                        }
                    });
                }
            }

            //@this.RenderTransformOrigin = new Point(.5, .5);
            double angle;

            switch (textualStyle.TextRotation)
            {
            case TextRotation.None:
                angle = 0;
                break;

            case TextRotation.Left:
                angle = -90;
                break;

            case TextRotation.Right:
                angle = 90;
                break;

            default:
                throw new InvalidEnumArgumentException(nameof(textualStyle.TextRotation), (int)textualStyle.TextRotation, typeof(TextRotation));
            }
            @this.LayoutTransform = new RotateTransform(angle, .5, .5);
        }