Last active
November 12, 2021 11:35
-
-
Save CheetahChrome/e21884ab06e85e27cb2c950ea0b01c60 to your computer and use it in GitHub Desktop.
A WPF ErrorBox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Expression.Interactivity.Core; | |
... | |
VM.ErrorCopyToClipboard = new ActionCommand((o) => | |
{ | |
if (!string.IsNullOrEmpty(VM.Error)) | |
Clipboard.SetText(VM.Error); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Assets used
check_green.png
close.png
Misc gifs for use in other projects to show running.