Class DialogBuilder<T>
Represents a builder to build dialogs.
public class DialogBuilder<T> where T : DialogViewModel, new()
Type Parameters
T
Type of the dialog view model.
- Inheritance
-
DialogBuilder<T>
- Inherited Members
Examples
General Example
This example shows how to show a dialog from Python and C#:
import clr
clr.AddReference("Loehnert.Controls")
from Loehnert.Controls.Dialogs import DialogService
result = DialogService.GetDialogBuilder()\
.WithTitle("Hallo")\
.WithMessage("Message1").Left()\
.WithImage(uri= "pack://application:,,,/Loehnert.Controls;component/icons/cavitywaitanimation.xaml").Height(120).Left()\
.WithImage(uri= "https://lisrt.de/images/lisrtv3websitelogo.png").Height(60)\
.WithMessage("Message2")\
.WithInput(int, "Integer input").Right()\
.WithInput(str, "String input")\
.WithInput(bool, "Bool input")\
.WithInput(float, "Float input")\
.WithCustomButton("CustomButton", "Custom Button")\
.WithYesButton()\
.WithNoButton().IsDefaultButton()\
.WithCancelButton()\
.ThrowExceptionOnCancel()\
.Build().Show()
# Print value of first input field.
print result.Values[0]
Asynchronous Dialogs
This example shows a dialog asynchronous:
dialog = DialogService.GetDialogBuilder()\
.WithTitle("Title")\
.WithYesButton().WithNoButton()\
.Build()
dialog.ShowAsync()
print "Dialog was opened"
# You can close the dialog from code
# dialog.Close()
# Wait until dialog is closed
task.Wait()
print "Dialog was closed"
result = task.Result # type: dialogs.DialogResult
print result.CloseReasonKey
Input Validation
This example shows how to validate a user input:
def validate_serial_number(input):
if (len(input) != 5):
return "Length must be 5"
result = DialogService.GetDialogBuilder()\
.WithTitle("Enter serial number")\
.WithInput(str, "Serial number", None, validate_serial_number)\
.WithYesButton().WithNoButton()\
.Build().Show()
Change Content of the Open Dialog
Add a view model, using the WithViewModel(ItemViewModelBase). You can change properties of the view model while the dialog is open You can use these view models types:
- BoolInputViewModel
- InputViewModel
- ImageViewModel
- MessageViewModel
- TitleViewModel
- A custom view model, that inherits from ImageViewModel using a custom view.
This example shows how to fill an input field programmatically:
from Loehnert.Controls.Dialogs import DialogService
from Loehnert.Controls.Dialogs.ViewModels import InputViewModel
input = InputViewModel(str)
input.Text = "Trace ID"
task = DialogService.GetDialogBuilder()\
.WithTitle("Enter or scan trace ID")\
.WithViewModel(input)\
.WithOkButton()\
.WithCancelButton()\
.ThrowExceptionOnCancel()\
.Build().ShowAsync()
time.sleep(2.0)
input.ValueAsString = "Scanned trace ID"
print task.Result.Values[0]
Multilingual Dialogs
The dialogs can be localized, using translations.
# -*- coding: utf-8 -*-
import clr
clr.AddReference("Loehnert.Controls")
clr.AddReference("Loehnert.Lisrt")
from Loehnert.Controls.Dialogs import DialogService
from Loehnert.Lisrt.Localization import Translation
title = Translation({'en': "Delete File", 'de': "Datei löschen"})
result = DialogService.GetDialogBuilder()\
.WithTitle(title)\
.WithMessage(Translation({'en': "Do you want to delete the file?", 'de': "Möchten Sie die Datei löschen?"}))\
.WithYesButton()\
.WithNoButton()\
.ThrowExceptionOnCancel()\
.Build().Show()
Constructors
DialogBuilder(IDialogCoordinator)
Initializes a new instance of the DialogBuilder<T> class.
public DialogBuilder(IDialogCoordinator dialogCoordinator)
Parameters
dialogCoordinator
IDialogCoordinatorCoordinator to open and close dialogs.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
Methods
Build()
Builds the dialog.
public Dialog Build()
Returns
- Dialog
The new dialog.
Center()
Sets the HorizontalAlignment of the last item to center.
public DialogBuilder<T> Center()
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- InvalidOperationException
Thrown when items are empty.
Height(double)
Sets the Height of the last item.
public DialogBuilder<T> Height(double height)
Parameters
height
doubleHeight in pixels of the last item.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- InvalidOperationException
Thrown when items are empty.
IsDefaultButton()
Configures the last added button as default button.
public DialogBuilder<T> IsDefaultButton()
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- InvalidOperationException
Thrown when no button is added.
Left()
Sets the HorizontalAlignment of the last item to left.
public DialogBuilder<T> Left()
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- InvalidOperationException
Thrown when items are empty.
Right()
Sets the HorizontalAlignment of the last item to right.
public DialogBuilder<T> Right()
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- InvalidOperationException
Thrown when items are empty.
ThrowExceptionOnCancel()
Configures that an exception is thrown when the user cancels the dialog.
public DialogBuilder<T> ThrowExceptionOnCancel()
Returns
- DialogBuilder<T>
A dialog builder.
UseFullScreen()
Defines that the dialog should be showed modally on the entire screen width.
public DialogBuilder<T> UseFullScreen()
Returns
- DialogBuilder<T>
A dialog builder.
WithCancelButton()
Adds a Cancel button to the dialog. The CloseReasonKey will be Cancel.
public DialogBuilder<T> WithCancelButton()
Returns
- DialogBuilder<T>
A dialog builder.
WithCancellationTokenSource(CancellationTokenSource)
Close the dialog when a cancellation is requested.
public DialogBuilder<T> WithCancellationTokenSource(CancellationTokenSource cancellationTokenSource)
Parameters
cancellationTokenSource
CancellationTokenSourceCancellation token source.
Returns
- DialogBuilder<T>
A dialog builder.
WithContext(object)
Sets the context (a view model) in which to show the dialog, this is required for external windows, for example in dialog windows. By default, the IMainWindow view model is used.
public DialogBuilder<T> WithContext(object context)
Parameters
context
objectThe context in which to show the dialog.
Returns
- DialogBuilder<T>
A dialog builder.
Remarks
The view, belonging to the context, must be registered for dialog participation.
WithCustomButton(string, object)
Adds a custom button to the dialog.
public DialogBuilder<T> WithCustomButton(string key, object text)
Parameters
key
stringKey of the button.
text
objectText of button, this can be an implementation of Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when
key
is null.
WithImage(string)
Adds a image to the dialog, see ImageViewModel. Change the hight with the Height(double) method.
public DialogBuilder<T> WithImage(string uri)
Parameters
uri
stringImage URI.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithInput(Type, object, object)
Adds an input field to the dialog, see InputViewModel, enums are supported.
public DialogBuilder<T> WithInput(Type inputType, object text, object prepopulatedValue = null)
Parameters
inputType
TypeType of the input value.
text
objectDescription of the input value, this can be an implementation of Gu.Localization.ITranslation.
prepopulatedValue
objectThe prepopulated field value. Use null for no prepopulated value.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithInput(Type, object, object, Func<object, string>)
Adds an input field to the dialog, see InputViewModel.
public DialogBuilder<T> WithInput(Type inputType, object text, object prepopulatedValue, Func<object, string> validate)
Parameters
inputType
TypeType of the input value. String, bool, numeric types and enums are supported.
text
objectDescription of the input value, this can be an implementation of Gu.Localization.ITranslation.
prepopulatedValue
objectThe prepopulated field value. Use null for no prepopulated value.
validate
Func<object, string>A function to validate the input. This function is called with Value and must return the validation message. If validation passes, the
validate
function should return null or an empty string.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithMessage(object)
Adds a message to the dialog, see MessageViewModel.
public DialogBuilder<T> WithMessage(object message)
Parameters
message
objectText of the message, this can be an implementation of Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithNoButton()
Adds a No button to the dialog. The CloseReasonKey will be No.
public DialogBuilder<T> WithNoButton()
Returns
- DialogBuilder<T>
A dialog builder.
WithOkButton()
Adds a OK button to the dialog. The CloseReasonKey will be OK.
public DialogBuilder<T> WithOkButton()
Returns
- DialogBuilder<T>
A dialog builder.
WithSelection(object, params object[])
Adds a selection input field to the dialog.
public DialogBuilder<T> WithSelection(object text, params object[] items)
Parameters
text
objectDescription of the input value, this can be an implementation of Gu.Localization.ITranslation.
items
object[]Items to select, can be of type Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithSettings(MetroDialogSettings)
Adds dialog setting to the dialog.
public DialogBuilder<T> WithSettings(MetroDialogSettings settings)
Parameters
settings
MetroDialogSettingsDialog settings.
Returns
- DialogBuilder<T>
A dialog builder.
WithTitle(object)
Sets the title of the dialog.
public DialogBuilder<T> WithTitle(object title)
Parameters
title
objectTitle of the dialog, this can be an implementation of Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithTitleError(object)
Adds a title with an error icon to the dialog.
public DialogBuilder<T> WithTitleError(object title)
Parameters
title
objectThe title, this can be an implementation of Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithTitleInfo(object)
Adds a title with an info icon to the dialog.
public DialogBuilder<T> WithTitleInfo(object title)
Parameters
title
objectThe title, this can be an implementation of Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithTitleWarning(object)
Adds a title with a warning icon to the dialog.
public DialogBuilder<T> WithTitleWarning(object title)
Parameters
title
objectThe title, this can be an implementation of Gu.Localization.ITranslation.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithViewModel(ItemViewModelBase)
Adds a custom view model to the dialog.
public DialogBuilder<T> WithViewModel(ItemViewModelBase itemViewModel)
Parameters
itemViewModel
ItemViewModelBaseView model to add.
Returns
- DialogBuilder<T>
A dialog builder.
Exceptions
- ArgumentNullException
Thrown when an argument is null.
WithYesButton()
Adds a Yes button to the dialog. The CloseReasonKey will be Yes.
public DialogBuilder<T> WithYesButton()
Returns
- DialogBuilder<T>
A dialog builder.