예제 #1
0
        private void PropertiesChanged()
        {
            this.Inlines.Clear();

            if (ContextMenu != null && Popup != null)
            {
                ContextMenu.Loaded += ContextMenu_Loaded;
                Popup.Loaded       += Popup_Loaded;
            }

            ReadLinksAndText(FormatText, (text, isLink) =>
            {
                text = text.Trim();
                if (!isLink)
                {
                    var run   = new Run(text);
                    run.Style = RunStyle;
                    this.Inlines.Add(run);
                }
                else
                {
                    var resolvedPropertyObject = DataItem.GetType().GetProperty(text).GetValue(DataItem, null);
                    var link         = new Hyperlink(new Run(resolvedPropertyObject.ToString()));
                    link.NavigateUri = new Uri("about:none");
                    link.Style       = HyperlinkStyle;

                    link.RequestNavigate += (s, e) =>
                    {
                        // Take focus now so that we get return focus when the user leaves.
                        link.Focus();
                        var dpiX = Window.GetWindow(this).DpiX();
                        var dpiY = Window.GetWindow(this).DpiY();

                        if (resolvedPropertyObject is IOptionViewModel)
                        {
                            ContextMenu2.Opacity     = 0;
                            ContextMenu2.ItemsSource = GetContextMenuFromOptionViewModel((IOptionViewModel)resolvedPropertyObject).OrderBy(menu => menu.DisplayName);
                            ContextMenu2.UpdateLayout();
                            ContextMenu2.IsOpen = true;
                            ContextMenu2.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                ContextMenu2.Opacity = 1;
                                ContextMenu2.HorizontalOffset = -1 * (ContextMenu2.RenderSize.Width / dpiX) / 2;
                                ContextMenu2.VerticalOffset = -1 * (ContextMenu2.RenderSize.Height / dpiY) / 2;
                                ContextMenu2.Focus();
                            }),
                                                                System.Windows.Threading.DispatcherPriority.DataBind, null);
                        }
                        else
                        {
                            Popup.PreviewKeyDown += (_, ee) =>
                            {
                                if (ee.Key == Key.Escape)
                                {
                                    Popup.IsOpen = false;
                                }
                            };
                            Popup.Opacity     = 0;
                            Popup.DataContext = resolvedPropertyObject;
                            Popup.UpdateLayout();
                            Popup.Child.UpdateLayout();
                            Popup.IsOpen = true;
                            Popup.Dispatcher.BeginInvoke((Action)(() =>
                            {
                                Popup.Opacity = 1;
                                Popup.HorizontalOffset = -1 * (Popup.Child.RenderSize.Width / dpiX) / 2;
                                Popup.VerticalOffset = -1 * (Popup.Child.RenderSize.Height / dpiY) / 2;
                                Keyboard.Focus(Popup.Child.FindVisualChild <Control>());
                            }),
                                                         System.Windows.Threading.DispatcherPriority.DataBind, null);
                        }
                    };
                    this.Inlines.Add(link);
                }
                this.Inlines.Add(new Run(" "));
            });
        }
예제 #2
0
 private void ContextMenu_Loaded(object sender, RoutedEventArgs e)
 {
     ContextMenu2.UpdateLayout();
     ContextMenu2.HorizontalOffset = -1 * ContextMenu2.RenderSize.Width / 2;
     ContextMenu2.VerticalOffset   = -1 * ContextMenu2.RenderSize.Height / 2;
 }