Пример #1
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (ButtonBase != null)
            {
                ButtonBase.Checked       -= CheckedHandler;
                ButtonBase.Unchecked     -= UncheckedHandler;
                ButtonBase.Indeterminate -= IndeterminateHandler;

                BindingOperations.ClearBinding(ButtonBase, ToggleButton.IsCheckedProperty);

                ButtonBase.IsEnabledChanged -= IsEnabledHandler;
            }

            ButtonBase = EnforceInstance <ToggleButton>("PART_Button");

            if (ButtonBase != null)
            {
                ButtonBase.Checked       += CheckedHandler;
                ButtonBase.Unchecked     += UncheckedHandler;
                ButtonBase.Indeterminate += IndeterminateHandler;

                var binding = new Binding("IsChecked")
                {
                    Source = this
                };
                ButtonBase.SetBinding(ToggleButton.IsCheckedProperty, binding);

                ButtonBase.IsEnabledChanged += IsEnabledHandler;

                RadioController?.AddElement(ButtonBase, RadioControllerParameter);
            }

            ChangeVisualState(false);
        }
        public static void RefreshButtonFromItsRichCommand(ButtonBase button, bool showTextLabel)
        {
            var command = button.Command as RichDelegateCommand;

            if (command == null)
            {
#if DEBUG
                Debugger.Break();
#endif
                return;
            }

            button.ToolTip = command.LongDescription +
                             (!String.IsNullOrEmpty(command.KeyGestureText) ? " " + command.KeyGestureText + " " : "");

            button.SetValue(AutomationProperties.NameProperty, UserInterfaceStrings.EscapeMnemonic(command.ShortDescription) + " / " + button.ToolTip);
            //button.SetValue(AutomationProperties.HelpTextProperty, command.ShortDescription);

            if (command.HasIcon && (!showTextLabel || String.IsNullOrEmpty(command.ShortDescription)))
            {
                var iconProvider = command.IconProviderNotShared;

                //button.Content = image;
                iconProvider.IconMargin_Medium = new Thickness(2, 2, 2, 2);

                var richButt = button as ButtonRichCommand;

                var binding = new Binding
                {
                    Mode   = BindingMode.OneWay,
                    Source = iconProvider,
                    Path   = new PropertyPath(
                        richButt != null && richButt.UseSmallerIcon ? PropertyChangedNotifyBase.GetMemberName(() => iconProvider.IconSmall) : PropertyChangedNotifyBase.GetMemberName(() => iconProvider.IconMedium)
                        )
                };

                var expr = button.SetBinding(Button.ContentProperty, binding);
            }
            else
            {
                if (button.Tag is ImageAndTextPlaceholder)
                {
                    object currentImageContent = ((ImageAndTextPlaceholder)button.Tag).m_ImageHost.Content;
                    if (currentImageContent is Image)
                    {
                        var image = currentImageContent as Image;
                        ((ImageAndTextPlaceholder)button.Tag).m_Command.IconProviderDispose(image);
                    }

                    if (command.HasIcon)
                    {
                        var iconProvider = command.IconProviderNotShared;

                        var binding = new Binding
                        {
                            Mode   = BindingMode.OneWay,
                            Source = iconProvider,
                            Path   =
                                new PropertyPath(
                                    PropertyChangedNotifyBase.GetMemberName(
                                        () => iconProvider.IconMedium))
                        };
                        var bindingExpressionBase_ =
                            ((ImageAndTextPlaceholder)button.Tag).m_ImageHost.SetBinding(
                                ContentControl.ContentProperty, binding);
                    }

                    ((ImageAndTextPlaceholder)button.Tag).m_TextHost.Content = command.ShortDescription;
                    button.ToolTip = command.LongDescription;

                    button.SetValue(AutomationProperties.NameProperty, UserInterfaceStrings.EscapeMnemonic(command.ShortDescription) + " / " + button.ToolTip);
                    //button.SetValue(AutomationProperties.HelpTextProperty, command.ShortDescription);

                    ((ImageAndTextPlaceholder)button.Tag).m_Command = command;
                }
                else
                {
                    button.Content = null;

                    var panel = new StackPanel
                    {
                        Orientation = Orientation.Horizontal
                    };

                    var imageHost = new ContentControl {
                        Focusable = false
                    };

                    if (command.HasIcon)
                    {
                        var iconProvider = command.IconProviderNotShared;

                        //Image image = command.IconProvider.IconMedium;
                        iconProvider.IconMargin_Medium = new Thickness(2, 2, 2, 2);

                        var binding = new Binding
                        {
                            Mode   = BindingMode.OneWay,
                            Source = iconProvider,
                            Path   =
                                new PropertyPath(
                                    PropertyChangedNotifyBase.GetMemberName(
                                        () => iconProvider.IconMedium))
                        };

                        var bindingExpressionBase = imageHost.SetBinding(ContentControl.ContentProperty, binding);
                    }

                    panel.Children.Add(imageHost);

                    var tb = new Label
                    {
                        VerticalAlignment = VerticalAlignment.Center,
                        Content           = command.ShortDescription,
                        //Margin = new Thickness(8, 0, 0, 0)
                    };

                    //tb.Content = new Run(UserInterfaceStrings.EscapeMnemonic(command.ShortDescription));

                    panel.Children.Add(tb);
                    button.Content = panel;

                    button.ToolTip = command.LongDescription;

                    button.SetValue(AutomationProperties.NameProperty, UserInterfaceStrings.EscapeMnemonic(command.ShortDescription) + " / " + button.ToolTip);
                    //button.SetValue(AutomationProperties.HelpTextProperty, command.ShortDescription);

                    button.Tag = new ImageAndTextPlaceholder(imageHost, tb)
                    {
                        m_Command = command
                    };
                }
            }
        }
        public static void ConfigureButtonFromCommand(ButtonBase button, RichDelegateCommand <object> command, bool showTextLabel)
        {
            button.Command = command;

            button.ToolTip = command.LongDescription +
                             (!String.IsNullOrEmpty(command.KeyGestureText) ? " " + command.KeyGestureText + " " : "");


            if (!showTextLabel || String.IsNullOrEmpty(command.ShortDescription))
            {
                //button.Content = image;
                command.IconProvider.IconMargin_Medium = new Thickness(2, 2, 2, 2);

                var binding = new Binding
                {
                    Mode   = BindingMode.OneWay,
                    Source = command.IconProvider,
                    Path   = new PropertyPath("IconMedium")
                };

                var expr = button.SetBinding(Button.ContentProperty, binding);
            }
            else
            {
                if (button.Tag is ImageAndTextPlaceholder)
                {
                    var binding = new Binding
                    {
                        Mode   = BindingMode.OneWay,
                        Source = command.IconProvider,
                        Path   = new PropertyPath("IconMedium")
                    };

                    var bindingExpressionBase_ = ((ImageAndTextPlaceholder)button.Tag).m_ImageHost.SetBinding(ContentControl.ContentProperty, binding);
                    ((ImageAndTextPlaceholder)button.Tag).m_TextHost.Content = command.ShortDescription;
                    button.ToolTip = command.LongDescription;
                }
                else
                {
                    button.Content = null;

                    //Image image = command.IconProvider.IconMedium;
                    command.IconProvider.IconMargin_Medium = new Thickness(2, 2, 2, 2);

                    var panel = new StackPanel
                    {
                        Orientation = Orientation.Horizontal
                    };

                    var imageHost = new ContentControl();

                    var binding = new Binding
                    {
                        Mode   = BindingMode.OneWay,
                        Source = command.IconProvider,
                        Path   = new PropertyPath("IconMedium")
                    };

                    var bindingExpressionBase = imageHost.SetBinding(ContentControl.ContentProperty, binding);

                    panel.Children.Add(imageHost);

                    var tb = new Label
                    {
                        VerticalAlignment = VerticalAlignment.Center,
                        Content           = command.ShortDescription,
                        //Margin = new Thickness(8, 0, 0, 0)
                    };

                    //tb.Content = new Run(UserInterfaceStrings.EscapeMnemonic(command.ShortDescription));

                    panel.Children.Add(tb);
                    button.Content = panel;

                    button.ToolTip = command.LongDescription;

                    button.Tag = new ImageAndTextPlaceholder
                    {
                        m_ImageHost = imageHost,
                        m_TextHost  = tb
                    };
                }
            }
        }