Zum Inhalt
Kuonen.UI

Dialoge (WinUI, WinForms)

ContentDialog in der Fensterfläche mit drei Knöpfen; ColorDialog und FontDialog wie WinForms als eigene Fenster. Das Ergebnis steht darunter.

Vorführung: Dialoge (WinUI, WinForms)Vorführung: Dialoge (WinUI, WinForms)

KXAML

DialogeSeite.kxaml
<StackPanel xmlns="https://ui.kuonen.org/kxaml"
            xmlns:x="https://ui.kuonen.org/kxaml/x"
            x:Class="Kuonen.UI.Galerie.Seiten.DialogeVorfuehrung"
            Spacing="8">
  <!-- Gefüllt von DialogeSeite.Fuelle, derselben Methode wie die C#-Fassung. -->
  <StackPanel x:Name="Flaeche" Spacing="8" />
</StackPanel>
DialogeSeite.kxaml.cs
using Kuonen.UI;

namespace Kuonen.UI.Galerie.Seiten;

public sealed partial class DialogeSeite
{
    protected override Func<UIElement>? KxamlFabrik => () => new DialogeVorfuehrung();
}

/// <summary>KXAML-Fassung der Seite: das Gerüst steht in DialogeSeite.kxaml, das Innere füllt DialogeSeite.Fuelle.</summary>
public sealed partial class DialogeVorfuehrung
{
    public DialogeVorfuehrung()
    {
        InitializeComponent();
        DialogeSeite.Fuelle(Flaeche);
    }
}

C#

DialogeSeite.cs
using Kuonen.UI;
using Kuonen.UI.Controls;
using Kuonen.UI.Layout;
using Kuonen.UI.Media;

namespace Kuonen.UI.Galerie.Seiten;

/// <summary>Galerie-Seite: Dialoge (WinUI, WinForms) (Plan 6m).</summary>
public sealed partial class DialogeSeite : GalerieSeite
{
    public override string Titel => "Dialoge (WinUI, WinForms)";

    public override string Beschreibung => "ContentDialog in der Fensterfläche mit drei Knöpfen; ColorDialog und FontDialog wie WinForms als eigene Fenster. Das Ergebnis steht darunter.";

    public override string Stichworte => "ContentDialog ColorDialog FontDialog CommonDialog ShowAsync ShowDialog Dialog Farbe Schrift";

    public override string Datei => nameof(DialogeSeite);

    public override UIElement Baue()
    {
        // Wie die KXAML-Fassung: eine Wurzel mit der Fläche darin.
        var flaeche = new StackPanel { Spacing = 8 };
        Fuelle(flaeche);
        return new StackPanel { Spacing = 8, Children = { flaeche } };
    }

    private static Button Knopf(string text, Action tun)
    {
        var k = new Button { Content = text };
        k.Click += (_, _) => tun();
        return k;
    }

    private static StackPanel Zeile(params UIElement[] kinder)
    {
        var z = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 6 };
        foreach (var k in kinder) z.Children.Add(k);
        return z;
    }

    /// <summary>Inhalt der Seite (auch von der KXAML-Fassung benutzt).</summary>
    public static void Fuelle(StackPanel flaeche)
    {
        var ergebnis = new TextBlock { Text = "Noch nichts gewählt." };
        Button? anker = null;
        Window? Fenster()
        {
            for (Visual? v = anker; v is not null; v = v.VisualParent)
                if (v is Window w) return w;
            return null;
        }
        anker = Knopf("ContentDialog", async () =>
        {
            var dialog = new ContentDialog
            {
                Title = "Änderungen speichern?",
                Content = new TextBlock { Text = "Sonst gehen sie verloren." },
                PrimaryButtonText = "Speichern",
                SecondaryButtonText = "Nicht speichern",
                CloseButtonText = "Abbrechen",
                DefaultButton = ContentDialogButton.Primary,
            };
            ergebnis.Text = "ContentDialog: " + await dialog.ShowAsync(anker!);
        });
        var farbe = new ColorDialog();
        var schrift = new FontDialog { ShowColor = true };
        flaeche.Children.Add(Zeile(
            anker,
            Knopf("ColorDialog", async () =>
            {
                if (Fenster() is { } w && await farbe.ShowDialogAsync(w)) ergebnis.Text = "Farbe: " + farbe.Color;
            }),
            Knopf("FontDialog", async () =>
            {
                if (Fenster() is { } w && await schrift.ShowDialogAsync(w)) ergebnis.Text = $"Schrift: {schrift.FontFamily}, {schrift.FontSize}, {schrift.FontWeight}";
            })));
        flaeche.Children.Add(ergebnis);
    }
}