Skip to content

Windows and Dialogs

Roman Shapiro edited this page Mar 2, 2020 · 10 revisions

Window

Window can be dragged across the desktop and closed either by clicking 'x' or pressing the Escape key. It has Closed event that fires when the window is closed.

I.e. following code creates and show simple window with button inside:

    Window window = new Window
    {
        Title = "Simple Window"
    };

    TextButton button = new TextButton
    {
        Text = "Push Me!",
        HorizontalAlignment = HorizontalAlignment.Center
    };

    window.Content = button;

    window.Closed += (s, a) => {
      // Called when window is closed
    };

    window.ShowModal();

It is equivalent to the MML:

    <Project>
      <Project.ExportOptions Namespace="Test" Class="Test" OutputPath="D:\Temp" />
      <Window Title="Simple Window" Left="408" Top="193">
        <TextButton Text="Push Me!" HorizontalAlignment="Center" />
      </Window>
    </Project>

And would result in following: images/WindowDialogs1.png

Dialog

Dialog is enhanced version of Window that also have "Ok" and "Cancel" button. It has Result(bool) property that indicates whether "Ok"(it also fires if Enter key is down) or "Cancel" was clicked. Following code creates simple "Enter Your Name" dialog:

    Dialog dialog = new Dialog
    {
        Title = "Enter Your Name"
    };

    var stackPanel = new HorizontalStackPanel
    {
        Spacing = 8
    };
    stackPanel.Proportions.Add(new Proportion
    {
        Type = ProportionType.Auto,
    });
    stackPanel.Proportions.Add(new Proportion
    {
        Type = ProportionType.Fill,
    });

    var label1 = new Label
    {
        Text = "Name:"
    };
    stackPanel.Widgets.Add(label1);

    var textBox1 = new TextBox();
    stackPanel.Widgets.Add(textBox1);

    dialog.Content = stackPanel;

    dialog.Closed += (s, a) => {
        if (!dialog.Result)
        {
            // Dialog was either closed or "Cancel" clicked
            return;
        }

        // "Ok" was clicked or Enter key pressed
        //
    };

    dialog.ShowModal();

It is equivalent to the following MML:

    <Project>
      <Dialog Title="Enter Your Name">
        <HorizontalStackPanel Spacing="8">
          <HorizontalStackPanel.Proportions>
            <Proportion Type="Auto" />
            <Proportion Type="Fill" />
          </HorizontalStackPanel.Proportions>
          <Label Text="Name:" />
          <TextBox />
        </HorizontalStackPanel>
      </Dialog>
    </Project>

And would result in following: images/WindowDialogs2.png

Clone this wiki locally