11-02-2022, 07:34 AM
// script ""
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
var b = new wpfBuilder("Window").WinSize(500);
var dialog1 = new WpfDialogInWindow(b.Window);
b.R.Add("Text", out TextBox text1).Focus();
b.R.AddButton("Show dialog", _ => _ShowDialogInWindow()).Margin("70");
b.R.AddOkCancel();
b.End();
if (!b.ShowDialog()) return;
void _ShowDialogInWindow() {
if (dialog1.Content == null) { //show the same content created once. Or you can set new/different content each time.
var b = new wpfBuilder(panelType: WBPanelType.Canvas).Size(120, 120); //the Canvas
b.Add<Label>("Dialog");
b.AddButton("Cancel", _ => dialog1.Show(false)).XY(25, 80, 70, 20);
b.End();
dialog1.Content = b.Panel;
dialog1.ClickToHide = true;
//dialog1.Dialog.BorderBrush = Brushes.Orange;
//dialog1.Dialog.BorderThickness = new(2);
//dialog1.Dialog.Background = Brushes.LightYellow;
}
dialog1.Show(true);
}
/// <summary>
/// Shows a dialog-like content in the center of a WPF window.
/// </summary>
/// <example>
/// <code><![CDATA[
/// using System.Windows;
/// using System.Windows.Controls;
/// using System.Windows.Controls.Primitives;
/// using System.Windows.Input;
/// using System.Windows.Media;
///
/// var b = new wpfBuilder("Window").WinSize(500);
/// var dialog1 = new WpfDialogInWindow(b.Window);
/// b.R.Add("Text", out TextBox text1).Focus();
/// b.R.AddButton("Show dialog", _ => _ShowDialogInWindow()).Margin("70");
/// b.R.AddOkCancel();
/// b.End();
/// if (!b.ShowDialog()) return;
///
/// void _ShowDialogInWindow() {
/// if (dialog1.Content == null) { //show the same content created once. Or you can set new/different content each time.
/// var b = new wpfBuilder().Width(200);
/// b.R.Add<Label>("Dialog");
/// b.R.AddButton("Cancel", _ => dialog1.Show(false));
/// b.End();
/// dialog1.Content = b.Panel;
/// dialog1.ClickToHide = true;
/// }
/// dialog1.Show(true);
/// }
/// ]]></code>
/// </example>
class WpfDialogInWindow {
wpfBuilder _b;
Window _window;
Grid _grid;
FrameworkElement _winContent;
Border _border;
Border _dim;
/// <summary>
/// Initializes this object.
/// </summary>
/// <param name="w">A WPF window.</param>
/// <exception cref="InvalidOperationException">w.Content is not set.</exception>
/// <remarks>
/// Replaces <i>w</i> content with a panel where adds <i>w</i> content and a hidden <b>Border</b> control (<see cref="Dialog"/>) that will display user-defined content (<see cref="Content"/>) when <see cref="Show"/> called.
/// </remarks>
public WpfDialogInWindow(Window w) {
_window = w;
_winContent = w.Content as FrameworkElement ?? throw new InvalidOperationException("w.Content is not set");
w.Content = null;
_b = new(w);
_grid = _b.Panel as Grid;
_grid.Margin = default;
_b.Columns(-1, 0, -1);
_b.Row(-1).Add(_winContent).Span(3).SpanRows(3);
_dim = new() { Background = Brushes.Black, Opacity = .6, Visibility = Visibility.Hidden };
Grid.SetColumnSpan(_dim, 3); Grid.SetRowSpan(_dim, 3);
_grid.Children.Add(_dim);
_dim.MouseLeftButtonDown += (_, _) => { if(ClickToHide) Show(false); };
_b.R.Skip().Add(out _border).Span(1).Hidden().Border(padding: new(5), cornerRadius: 5).Brush(Brushes.White);
_b.Row(-1);
_b.End();
}
/// <summary>
/// Gets the dialog content container control. You can set its properties, eg background color.
/// </summary>
public Border Dialog => _border;
/// <summary>
/// Sets or gets dialog content.
/// </summary>
public FrameworkElement Content { get; set; }
/// <summary>
/// true if the dialog is visible.
/// </summary>
public bool IsVisible => _border.IsVisible;
/// <summary>
/// Hide the dialog when the user clicks the dimmed area.
/// </summary>
public bool ClickToHide { get; set; }
/// <summary>
/// Shows or hides the dialog.
/// </summary>
/// <param name="show"></param>
public void Show(bool show) {
if (show == IsVisible) return;
_border.Child = show ? Content : null;
_border.Visibility = show ? Visibility.Visible : Visibility.Hidden;
_dim.Visibility = show ? Visibility.Visible : Visibility.Hidden;
_winContent.IsEnabled = !show;
}
}