Skip to content

Instantly share code, notes, and snippets.

@DamianSuess
Created February 5, 2023 01:16
Show Gist options
  • Save DamianSuess/c8dda042b483e8f807cd15ad7ff3988b to your computer and use it in GitHub Desktop.
Save DamianSuess/c8dda042b483e8f807cd15ad7ff3988b to your computer and use it in GitHub Desktop.
Prism.Avalonia - MessageDialogView
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
MaxHeight="{Binding MaxHeight}"
MaxWidth="{Binding MaxWidth}"
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="150"
x:Class="SampleApp.Views.MessageDialogView">
<UserControl.Styles>
<Style Selector="TextBox">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Margin" Value="15" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="AcceptsReturn" Value="True" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="CaretBrush" Value="Transparent" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
</Style>
<Style Selector="TextBox:pointerover /template/ Border#border">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="TextBox:pointerover /template/ Border#PART_BorderElement">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="TextBox:focus /template/ Border#border">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="TextBox:focus /template/ Border#PART_BorderElement">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="TextBox.header">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="0,0,0,10" />
</Style>
<Style Selector="Button">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Margin" Value="15" />
<Setter Property="MinHeight" Value="24" />
<Setter Property="MinWidth" Value="75" />
</Style>
</UserControl.Styles>
<Grid ColumnDefinitions="Auto,*" RowDefinitions="*,15,Auto">
<TextBox Grid.Row="0"
Grid.Column="1"
Text="{Binding CustomMessage}" />
<!-- ButtonResult 1=OK -->
<Button Content="OK"
Command="{Binding CmdResult}"
CommandParameter="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="2"
HorizontalAlignment="Right"/>
</Grid>
<!--
<Button Content="None" Command="{Binding CmdResult}" CommandParameter="0" />
<Button Content="OK" Command="{Binding CmdResult}" CommandParameter="1" />
<Button Content="Cancel" Command="{Binding CmdResult}" CommandParameter="2" />
<Button Content="Abort" Command="{Binding CmdResult}" CommandParameter="3" />
<Button Content="Retry" Command="{Binding CmdResult}" CommandParameter="4" />
<Button Content="Ignore" Command="{Binding CmdResult}" CommandParameter="5" />
<Button Content="Yes" Command="{Binding CmdResult}" CommandParameter="6" />
<Button Content="No" Command="{Binding CmdResult}" CommandParameter="7" />
-->
</UserControl>
using System;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
namespace SampleApp.ViewModels;
public class MessageDialogViewModel : BindableBase, IDialogAware
{
private string _customMessage = string.Empty;
private string _title = "Notification";
private int _maxHeight;
private int _maxWidth;
public MessageDialogViewModel()
{
// Since this is a basic ShellWindow, there's nothing
// to do here.
// For enterprise apps, you could register up subscriptions
// or other startup background tasks so that they get triggered
// on startup, rather than putting them in the DashboardViewModel.
//
// For example, initiate the pulling of News Feeds, etc.
Title = "Alert!";
MaxHeight = 800;
MaxWidth = 600;
}
public event Action<IDialogResult>? RequestClose;
public string Title
{
get => _title;
set => SetProperty(ref _title, value);
}
public int MaxHeight
{
get => _maxHeight;
set => SetProperty(ref _maxHeight, value);
}
public int MaxWidth
{
get => _maxWidth;
set => SetProperty(ref _maxWidth, value);
}
public DelegateCommand<string> CmdResult => new DelegateCommand<string>((param) =>
{
// None = 0
// OK = 1
// Cancel = 2
// Abort = 3
// Retry = 4
// Ignore = 5
// Yes = 6
// No = 7
ButtonResult result = ButtonResult.None;
if (int.TryParse(param, out int intResult))
result = (ButtonResult)intResult;
RaiseRequestClose(new DialogResult(result));
});
public string CustomMessage
{
get => _customMessage;
set => SetProperty(ref _customMessage, value);
}
public virtual bool CanCloseDialog()
{
// Allow the dialog to close
return true;
}
public virtual void OnDialogClosed()
{
// Detatch custom eventhandlers here, etc.
}
public void OnDialogOpened(IDialogParameters parameters)
{
var title = parameters.GetValue<string>("title");
if (!string.IsNullOrEmpty(title))
Title = title;
CustomMessage = parameters.GetValue<string>("message");
}
public virtual void RaiseRequestClose(IDialogResult dialogResult)
{
RequestClose?.Invoke(dialogResult);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment