Beispiel #1
0
        private static void HandleContextMenuOpeningStatic(object sender, ContextMenuEventArgs e)
        {
            DependencyObject dObjSender         = sender as DependencyObject;
            IInputElement    inputElementSender = sender as IInputElement;

            if (dObjSender != null && inputElementSender != null)
            {
                ContextMenu contextMenu = (ContextMenu)(dObjSender.GetValue(FrameworkElement.ContextMenuProperty));

                if (contextMenu == null)
                {
                    TextSelectionSettings         settings = TextSelection.GetSettings(dObjSender);
                    IEnumerable <RoutedUICommand> commands = null;

                    if (settings != null && settings.HasContextMenuCommands)
                    {
                        commands = settings.ContextMenuCommands;
                    }
                    else
                    {
                        commands = TextSelectionSettings.DefaultContextMenuCommands;
                    }

                    contextMenu = CreateContextMenu(inputElementSender, commands);

                    if (contextMenu != null)
                    {
                        // We are repacing a null ContextMenu with our built from scratch ContextMenu.
                        // Mark the event as handled so that the built in context menu service does not
                        // try to open the null Context menu and we can open our built from scratch menu
                        // ourselves
                        e.Handled = true;

                        RoutedEventHandler handleContextMenuClosed = null;
                        handleContextMenuClosed = (closedSender, closedArgs) =>
                        {
                            inputElementSender.RemoveHandler(FrameworkElement.ContextMenuClosingEvent, handleContextMenuClosed);
                            dObjSender.ClearValue(FrameworkElement.ContextMenuProperty);
                        };

                        inputElementSender.AddHandler(FrameworkElement.ContextMenuClosingEvent, handleContextMenuClosed);
                        dObjSender.SetValue(FrameworkElement.ContextMenuProperty, contextMenu);
                        contextMenu.IsOpen = true;
                    }
                }
            }
        }
Beispiel #2
0
        private TextSelectionSettings EnsureSettings()
        {
            TextSelectionSettings result;

            if (this.textBlock != null)
            {
                result = TextSelection.GetSettings(this.textBlock);
                if (result == null)
                {
                    result = new TextSelectionSettings();
                    TextSelection.SetSettings(this.textBlock, result);
                }
            }
            else
            {
                result = new TextSelectionSettings();
            }

            return(result);
        }
Beispiel #3
0
 public static void SetSettings(DependencyObject obj, TextSelectionSettings value)
 {
     obj.SetValue(SettingsProperty, value);
 }
Beispiel #4
0
        protected virtual void OnPrepareHighlightAdorner(HighlightAdorner adorner)
        {
            // TODO: figure out how to create a binding to "(TextSelection.Settings).HighlightFillProperty" in code

            TextSelectionSettings settings = EnsureSettings();

            BindingBase isFocusedBinding = new Binding()
            {
                Path   = new PropertyPath(FrameworkElement.IsFocusedProperty),
                Source = this.textBlock,
                Mode   = BindingMode.OneWay
            };

            adorner.SetBinding(
                HighlightAdorner.FillProperty,
                new MultiBinding()
            {
                Bindings =
                {
                    isFocusedBinding,
                    new Binding()
                    {
                        Path   = new PropertyPath(TextSelectionSettings.HighlightFillProperty),
                        Source = settings,
                        Mode   = BindingMode.OneWay
                    },
                    new Binding()
                    {
                        Path   = new PropertyPath(TextSelectionSettings.InactiveHighlightFillProperty),
                        Source = settings,
                        Mode   = BindingMode.OneWay
                    }
                },
                Mode      = BindingMode.OneWay,
                Converter = ToggleMultiConverter.Default
            }
                );

            adorner.SetBinding(
                HighlightAdorner.StrokeProperty,
                new MultiBinding()
            {
                Bindings =
                {
                    isFocusedBinding,
                    new Binding()
                    {
                        Path   = new PropertyPath(TextSelectionSettings.HighlightStrokeProperty),
                        Source = settings,
                        Mode   = BindingMode.OneWay
                    },
                    new Binding()
                    {
                        Path   = new PropertyPath(TextSelectionSettings.InactiveHighlightStrokeProperty),
                        Source = settings,
                        Mode   = BindingMode.OneWay
                    }
                },
                Mode      = BindingMode.OneWay,
                Converter = ToggleMultiConverter.Default
            }
                );
        }