Skip to content

Commit

Permalink
Published Protocol Data Validation
Browse files Browse the repository at this point in the history
Added data validation and automatic closing to the published protocol form. Not an elegant solution at the moment, but it works!
  • Loading branch information
Philip-S-Martin committed Feb 18, 2021
1 parent 3b468bd commit 3713407
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ProtocolMasterWPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<ResourceDictionary>
<helpers:NotNullToBoolConverter x:Key="NotNullToBoolConverter"/>
<helpers:NotNullToBoolConverter x:Key="NullOrEmptyStringToBoolConverter"/>
<helpers:IsEnabledConverter x:Key="IsEnabledConverter"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Expand Down
32 changes: 32 additions & 0 deletions ProtocolMasterWPF/Helpers/IsEnabledConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;

namespace ProtocolMasterWPF.Helpers
{
public class IsEnabledConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (values.LongLength > 0)
{
foreach (var value in values)
{
if (value is bool && (bool)value)
{
return false;
}
}
}
return true;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
18 changes: 18 additions & 0 deletions ProtocolMasterWPF/Helpers/NotEmptyValidationRule.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Controls;

namespace ProtocolMasterWPF.Helpers
{
public class NotEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
return string.IsNullOrWhiteSpace((value ?? "").ToString())
? new ValidationResult(false, "Field is required.")
: ValidationResult.ValidResult;
}
}
}
37 changes: 26 additions & 11 deletions ProtocolMasterWPF/View/PublishedSelectView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ProtocolMasterWPF.View"
xmlns:helpers="clr-namespace:ProtocolMasterWPF.Helpers"

xmlns:model="clr-namespace:ProtocolMasterWPF.Model" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
Expand All @@ -21,13 +23,12 @@
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<materialDesign:PopupBox StaysOpen="True" Grid.Column="0" PlacementMode="RightAndAlignBottomEdges" Margin="0,0,12,0">
<materialDesign:PopupBox x:Name="AddPopup" StaysOpen="True" Grid.Column="0" PlacementMode="RightAndAlignBottomEdges" Margin="0,0,12,0">
<materialDesign:PopupBox.ToggleContent>
<materialDesign:PackIcon Foreground="{DynamicResource MaterialDesignDarkForeground}" Kind="Add" Margin="10" Height="24" Width="24"/>
</materialDesign:PopupBox.ToggleContent>
<Grid Width="300" Margin="8,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
Expand All @@ -36,15 +37,29 @@
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Style="{DynamicResource MaterialDesignBody2TextBlock}" Text="Add Published File" HorizontalAlignment="Center"/>
<TextBlock Grid.Row="1" Grid.Column="0" Text="Name:" VerticalAlignment="Center" Margin="8"/>
<TextBox x:Name="NewNameText" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="8"/>
<TextBlock Grid.Row="2" Grid.Column="0" Text="URL:" VerticalAlignment="Center" Margin="8"/>
<TextBox x:Name="NewURLText" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center" Margin="8"/>
<StackPanel Grid.Row="3" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="8">
<Button Style="{DynamicResource MaterialDesignFlatMidBgButton}" Click="AddButton_Click">
<TextBlock>Add New</TextBlock>
</Button>
<TextBlock Grid.Row="0" Style="{DynamicResource MaterialDesignBody2TextBlock}" Text="New Published File" HorizontalAlignment="Center"/>
<TextBox x:Name="NewNameText" materialDesign:HintAssist.Hint="Protocol Name" materialDesign:ValidationAssist.OnlyShowOnFocus="True"
Grid.Row="1" VerticalAlignment="Center" Margin="8" Validation.Error="HandleValidationError">
<TextBox.Text>
<Binding Path="NewName" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
<Binding.ValidationRules>
<helpers:NotEmptyValidationRule ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox x:Name="NewURLText" materialDesign:HintAssist.Hint="https://protocol.url/download.file" materialDesign:ValidationAssist.OnlyShowOnFocus="True"
Grid.Row="2" VerticalAlignment="Center" Margin="8" Validation.Error="HandleValidationError">
<TextBox.Text>
<Binding Path="NewURL" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True">
<Binding.ValidationRules>
<helpers:NotEmptyValidationRule ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<StackPanel Grid.Row="3" Orientation="Horizontal" HorizontalAlignment="Right" Margin="8">
<Button Style="{DynamicResource MaterialDesignFlatMidBgButton}" x:Name="AddButton" Click="AddButton_Click" Content="Save"/>
</StackPanel>
</Grid>
</materialDesign:PopupBox>
Expand Down
36 changes: 35 additions & 1 deletion ProtocolMasterWPF/View/PublishedSelectView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ProtocolMasterWPF.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -19,14 +20,47 @@ namespace ProtocolMasterWPF.View
/// </summary>
public partial class PublishedSelectView : UserControl, ISelectView
{
public static readonly DependencyProperty NewNameProperty =
DependencyProperty.Register("NewName", typeof(string), typeof(PublishedSelectView));
public string NewName
{
get { return (string)GetValue(NewNameProperty); }
set { SetValue(NewNameProperty, value); }
}
public static readonly DependencyProperty NewURLProperty =
DependencyProperty.Register("NewURL", typeof(string), typeof(PublishedSelectView));
public string NewURL
{
get { return (string)GetValue(NewURLProperty); }
set { SetValue(NewURLProperty, value); }
}

private int _numberOfValidationErrors = 0;
public bool HasNoValidationErrors => _numberOfValidationErrors == 0;

private void HandleValidationError(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
_numberOfValidationErrors++;
else
_numberOfValidationErrors--;
AddButton.IsEnabled = HasNoValidationErrors;
}
public PublishedSelectView()
{
InitializeComponent();
}

public ListBox SelectList => SelectListBox;


private void DeleteButton_Click(object sender, RoutedEventArgs e) => PublishedFileStore.Instance.Remove(SelectListBox.SelectedValue as PublishedFileStreamer);
private void AddButton_Click(object sender, RoutedEventArgs e) => PublishedFileStore.Instance.Add(NewNameText.Text, NewURLText.Text);
private void AddButton_Click(object sender, RoutedEventArgs e)
{
PublishedFileStore.Instance.Add(NewName, NewURL);
NewName = "";
NewURL = "";
AddPopup.IsPopupOpen = false;
}
}
}

0 comments on commit 3713407

Please sign in to comment.