Skip to content

Instantly share code, notes, and snippets.

@CheetahChrome
Last active November 12, 2021 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CheetahChrome/e21884ab06e85e27cb2c950ea0b01c60 to your computer and use it in GitHub Desktop.
Save CheetahChrome/e21884ab06e85e27cb2c950ea0b01c60 to your computer and use it in GitHub Desktop.
A WPF ErrorBox
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibility" /> <!-- System.Windows.Controls. -->
</Windown.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="200*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
...
<Grid x:Name="ErrorBoxSection" Grid.Row="2" Margin="20"
Visibility="{Binding HasError, Converter={StaticResource BooleanToVisibility}}">
<Border BorderBrush="Black" BorderThickness="1" Background="White" d:IsHidden="True">
<Border.Effect>
<DropShadowEffect ShadowDepth="2"/>
</Border.Effect>
</Border>
<Border BorderBrush="DarkRed" BorderThickness=".5"
x:Name="BorderError">
<DockPanel>
<Button DockPanel.Dock="Top" HorizontalAlignment="Right" Command="{Binding ClearError}"
Margin="0,4,4,0"
BorderThickness="0"
Background="Transparent"
>
<Image Height="16" Width="16" Source="Assets/Close.png"/>
</Button>
<TextBlock x:Name="ErrorBox" TextWrapping="Wrap" Margin="12, -12, 12, 12" Foreground="Red" Text="{Binding Error}">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Command="{Binding ErrorCopyToClipboard}" Header="Copy To Clipboard">
<MenuItem.Icon>
<Image Source="assets/check_green.png" Height="16"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Command="{Binding ClearError}" Header="Close">
<MenuItem.Icon>
<Image Source="assets/close.png" Height="16"/>
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DockPanel>
</Border>
</Grid>
</Grid>
using Microsoft.Expression.Interactivity.Core;
...
VM.ErrorCopyToClipboard = new ActionCommand((o) =>
{
if (!string.IsNullOrEmpty(VM.Error))
Clipboard.SetText(VM.Error);
});
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Microsoft.Expression.Interactivity.Core;
public class MainVM : INotifiedPropertyChanged
{
private string _Error;
public string Error
{
get => _Error;
set { _Error = value; OnPropertyChanged(nameof(Error)); OnPropertyChanged(nameof(HasError)); }
}
public bool HasError => !string.IsNullOrEmpty(Error);
// Install-Package System.Windows.Interactivity.WPF for ICommand
public ICommand ClearError => new ActionCommand(() => { Error = string.Empty; });
public ICommand ErrorCopyToClipboard { get; set; }
/// <summary>
/// Event raised when a property changes.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises the PropertyChanged event.
/// </summary>
/// <param name="propertyName">The name of the property that has changed.</param>
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
@CheetahChrome
Copy link
Author

CheetahChrome commented Jun 10, 2021

Assets used

  • check_green.png check_green.png

  • close.png Close

Misc gifs for use in other projects to show running.

RunningYellow
RunningGreen
RunningRed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment