C
C#15mo ago
Elio

WPF Validation

Hi, I'm struggling with the validation part of my project. I've created this Viewmodel to experiment the validation. But now i do not understand how can i make a dialog to get back individually radius or roll position using the validation from this viewmodel. Here is my DialogView which is openned when i double click on the textbox which display radius or rollposition:
5 Replies
Elio
ElioOP15mo ago
Elio
ElioOP15mo ago
public class SymmetricRollViewModel : ViewModelModelBase<SymmetricRoll, SymmetricRollViewModel>, INotifyDataErrorInfo
{
public string Error
{
get { return string.Empty; }
}
public Dictionary<string, string> ErrorCollection { get; private set; } = new Dictionary<string, string>();
public string this[string columnName]
{
get
{
string result = string.Empty;

switch(columnName)
{
case "RollPosition":
if(string.IsNullOrWhiteSpace(RollPosition))
result = "RollPosition cannot be empty";
if(double.TryParse(RollPosition, out double value))
if(value > 10)
result = $"value false {value}";
break;
}

if(ErrorCollection.ContainsKey(columnName))
ErrorCollection[columnName] = result;
else if(result != null)
ErrorCollection.Add(columnName, result);

OnPropertyChanged(nameof(ErrorCollection));
return result;
}
}
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
public bool HasErrors
{
get { return false; }
}

private double _rollPosition;
public double RollPosition
{
get => _rollPosition;
set
{
_rollPosition = value;
OnPropertyChanged();
}
}

private double _radius;
public double Radius
{
get { return _radius; }
set
{
_radius = value;
OnPropertyChanged();
}
}

public long WidthId => _symmetricRoll.WidthId;
}
public class SymmetricRollViewModel : ViewModelModelBase<SymmetricRoll, SymmetricRollViewModel>, INotifyDataErrorInfo
{
public string Error
{
get { return string.Empty; }
}
public Dictionary<string, string> ErrorCollection { get; private set; } = new Dictionary<string, string>();
public string this[string columnName]
{
get
{
string result = string.Empty;

switch(columnName)
{
case "RollPosition":
if(string.IsNullOrWhiteSpace(RollPosition))
result = "RollPosition cannot be empty";
if(double.TryParse(RollPosition, out double value))
if(value > 10)
result = $"value false {value}";
break;
}

if(ErrorCollection.ContainsKey(columnName))
ErrorCollection[columnName] = result;
else if(result != null)
ErrorCollection.Add(columnName, result);

OnPropertyChanged(nameof(ErrorCollection));
return result;
}
}
public event EventHandler<DataErrorsChangedEventArgs>? ErrorsChanged;
public bool HasErrors
{
get { return false; }
}

private double _rollPosition;
public double RollPosition
{
get => _rollPosition;
set
{
_rollPosition = value;
OnPropertyChanged();
}
}

private double _radius;
public double Radius
{
get { return _radius; }
set
{
_radius = value;
OnPropertyChanged();
}
}

public long WidthId => _symmetricRoll.WidthId;
}
in the end i want a reusable dialog which used the validation of the properties
Mayor McCheese
Mayor McCheese15mo ago
This is a pretty complex topic tbh; take a look at this video: https://www.youtube.com/watch?v=AfhK-uM_7ls I just scanned it quickly, I think it's what you're looking to do; at least similarly.
Elio
ElioOP15mo ago
ok thanks i will take a look cause in fact my problem is how to manage some viewmodel that have lot of more properties, and i don't display a all of them just to modify one.
Mayor McCheese
Mayor McCheese15mo ago
Oh; I thought you were trying to do error validation adornment
Want results from more Discord servers?
Add your server