private void OnTextBlockRightTapped(object sender, RightTappedRoutedEventArgs e) {
            StackPanel stackPanel = new StackPanel();

            // Create two Button controls and add to StackPanel
            Button btn1 = new Button {
                Content = "Larger font",
                Tag = 1.2,
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(12)
            };
            btn1.Click += OnButtonClick;
            stackPanel.Children.Add(btn1);

            Button btn2 = new Button {
                Content = "Smaller font",
                Tag = 1 / 1.2,
                HorizontalAlignment = HorizontalAlignment.Center,
                Margin = new Thickness(12)
            };
            btn2.Click += OnButtonClick;
            stackPanel.Children.Add(btn2);

            // Create three RadioButton controls and add to StackPanel
            string[] names = { "Red", "Green", "Blue" };
            Color[] colors = { Colors.Red, Colors.Green, Colors.Blue };

            for (int i = 0; i < names.Length; i++) {
                RadioButton radioButton = new RadioButton {
                    Content = names[i],
                    Foreground = new SolidColorBrush(colors[i]),
                    IsChecked = (txtblk.Foreground as SolidColorBrush).Color == colors[i],
                    Margin = new Thickness(12)
                };
                radioButton.Checked += OnRadioButtonChecked;
                stackPanel.Children.Add(radioButton);
            }

            // Create a Border for the StackPanel
            Border border = new Border {
                Child = stackPanel,
                Background = this.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush,
                BorderBrush = this.Resources["ApplicationForegroundThemeBrush"] as SolidColorBrush,
                BorderThickness = new Thickness(1),
                Padding = new Thickness(24),
            };

            // Create the Popup object
            Popup popup = new Popup {
                Child = border,
                IsLightDismissEnabled = true
            };

            // Adjust location based on content size
            border.Loaded += (loadedSender, loadedArgs) => {
                Point point = e.GetPosition(this);
                point.X -= border.ActualWidth / 2;
                point.Y -= border.ActualHeight;

                // Leave at least a quarter inch margin
                popup.HorizontalOffset =
                    Math.Min(this.ActualWidth - border.ActualWidth - 24,
                    Math.Max(24, point.X));

                popup.VerticalOffset =
                    Math.Min(this.ActualHeight - border.ActualHeight - 24,
                    Math.Max(24, point.Y));

                // Set keyboard focus to first element
                btn1.Focus(FocusState.Programmatic);
            };

            // Open the popup
            popup.IsOpen = true;
        }
Пример #2
0
        private UIElement CreateDialog()
        {
            var content = Window.Current.Content as FrameworkElement;
            if (content == null)
            {
                // The dialog is being shown before content has been created for the window
                Window.Current.Activated += OnWindowActivated;
                return null;
            }

            Style subHeaderTextStyle = Application.Current.Resources["LightSubheaderTextStyle"] as Style;
            Style buttonStyle = Application.Current.Resources["LightButtonStyle"] as Style;

            double width = Window.Current.Bounds.Width;
            double height = Window.Current.Bounds.Height;
            var root = new Grid { Width = width, Height = height };
            var overlay = new Grid { Background = new SolidColorBrush(Colors.Black), Opacity = 0.2D };
            root.Children.Add(overlay);

            var dialogPanel = new Grid { VerticalAlignment = VerticalAlignment.Center };
            dialogPanel.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
            dialogPanel.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            dialogPanel.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
            dialogPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            dialogPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(2, GridUnitType.Star) });
            dialogPanel.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

            var titleBorder = new Border { Background = HeaderBrush, Height = 70D };
            Grid.SetColumnSpan(titleBorder, 3);
            dialogPanel.Children.Add(titleBorder);

            var titleTextBlock = new TextBlock();
            titleTextBlock.Text = Title;
            titleTextBlock.Style = subHeaderTextStyle;
            titleTextBlock.Margin = new Thickness(0, 0, 0, 20);
            titleTextBlock.VerticalAlignment = VerticalAlignment.Bottom;
            Grid.SetColumn(titleTextBlock, 1);
            dialogPanel.Children.Add(titleTextBlock);

            Border contentBorder = new Border { Background = new SolidColorBrush(Colors.White) };
            Grid.SetRow(contentBorder, 1);
            Grid.SetRowSpan(contentBorder, 2);
            Grid.SetColumnSpan(contentBorder, 3);
            dialogPanel.Children.Add(contentBorder);

            var contentPresenter = new ContentPresenter();
            contentPresenter.Content = Content;
            Grid.SetColumn(contentPresenter, 1);
            Grid.SetRow(contentPresenter, 1);
            dialogPanel.Children.Add(contentPresenter);

            _buttonPanel = new StackPanel { HorizontalAlignment = HorizontalAlignment.Right, Orientation = Orientation.Horizontal };
            Grid.SetRow(_buttonPanel, 2);
            Grid.SetColumn(_buttonPanel, 1);
            dialogPanel.Children.Add(_buttonPanel);

            if (Commands.Count == 0)
            {
                Button button = new Button();
                button.Style = buttonStyle;
                button.Content = "Close";
                button.MinWidth = 92;
                button.Margin = new Thickness(20, 20, 0, 20);
                button.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                button.Click += (okSender, okArgs) => CloseDialog(null);
                _buttonPanel.Children.Add(button);
            }
            else
            {
                bool flag = true;
                foreach (var command in Commands)
                {
                    IUICommand currentCommand = command;
                    Button button = new Button();
                    button.Style = buttonStyle;
                    button.Content = command.Label;
                    button.Margin = new Thickness(20, 20, 0, 20);
                    button.MinWidth = 92;
                    if (flag)
                    {
                        button.Focus(Windows.UI.Xaml.FocusState.Programmatic);
                        flag = false;
                    }
                    button.Click += (okSender, okArgs) => CloseDialog(currentCommand);
                    _buttonPanel.Children.Add(button);
                }
            }

            root.Children.Add(dialogPanel);
            return root;
        }