WPF grid with validation rule and dependency

At the moment I have a grid and I'm trying to have a cell with validation rules. To test it, I need the value of min and max of the string.

Validation Class:

public decimal Max { get; set; }

public decimal Min { get; set; }

public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
    var test = i < Min;
    var test2 = i > Max;

    if (test || test2)
        return new ValidationResult(false, String.Format("Fee out of range Min: ${0} Max: ${1}", Min, Max));
    else
        return new ValidationResult(true, null);
}

User control:

<telerik:RadGridView SelectedItem ="{Binding SelectedScript}"
                     ItemsSource="{Binding ScheduleScripts}">
    <telerik:RadGridView.Columns>
        <telerik:GridViewDataColumn
            DataMemberBinding="{Binding Amount}" Header="Amount" 
            CellTemplate="{StaticResource AmountDataTemplate}" 
            CellEditTemplate="{StaticResource AmountDataTemplate}"/>   
        <telerik:GridViewComboBoxColumn
            Header="Fee Type" 
            Style="{StaticResource FeeTypeScriptStyle}" 
            CellTemplate="{StaticResource FeeTypeTemplate}"/>           
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

FeeType Class:

public class FeeType
{
    public decimal Min { get; set; }
    public decimal Max { get; set; }
    public string Name { get; set; }
}

I tried this solution here with a WPF ValidationRule with a dependency property and it works fine. But now I am facing the problem that the proxy server cannot be created through the viewmodel. It is based on the row selected for the ComboBox Value Min and Max property.

For example, these sample values ​​with the list below

Admin Min: $75 Max $500
Late  Min: $0  Max $50

Since the grid can have almost as many rows as it wants, I don’t see how proxies are created in my situation. If I can get some leadership tips, we will be very grateful.

+6
1

Alert: , , ViewModels.

semplicity FeeTypes FeeType:

public class FeeType
{
    public decimal Min { get; set; }
    public decimal Max { get; set; }
    public string Name { get; set; }

    public static readonly FeeType[] List = new[]
    {
        new FeeType { Min = 0, Max = 10, Name = "Type1", },
        new FeeType { Min = 2, Max = 20, Name = "Type2", },
    };
}

ViewModel Grid. .

public class RowViewModel : INotifyPropertyChanged, INotifyDataErrorInfo
{
    public RowViewModel()
    {
        _errorFromProperty = new Dictionary<string, string>
        {
            { nameof(Fee), null },
            { nameof(Amount), null },
        };

        PropertyChanged += OnPropertyChanged;
    }

    private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch(e.PropertyName)
        {
            case nameof(Fee):
                OnFeePropertyChanged();
                break;
            case nameof(Amount):
                OnAmountPropertyChanged();
                break;
            default:
                break;
        }
    }

    private void OnFeePropertyChanged()
    {
        if (Fee == null)
            _errorFromProperty[nameof(Fee)] = "You must select a Fee!";
        else
            _errorFromProperty[nameof(Fee)] = null;

        NotifyPropertyChanged(nameof(Amount));
    }

    private void OnAmountPropertyChanged()
    {
        if (Fee == null)
            return;

        if (Amount < Fee.Min || Amount > Fee.Max)
            _errorFromProperty[nameof(Amount)] = $"Amount must be between {Fee.Min} and {Fee.Max}!";
        else
            _errorFromProperty[nameof(Amount)] = null;
    }

    public decimal Amount
    {
        get { return _Amount; }
        set
        {
            if (_Amount != value)
            {
                _Amount = value;
                NotifyPropertyChanged();
            }
        }
    }
    private decimal _Amount;

    public FeeType Fee
    {
        get { return _Fee; }
        set
        {
            if (_Fee != value)
            {
                _Fee = value;
                NotifyPropertyChanged();
            }
        }
    }
    private FeeType _Fee;

    #region INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion

    #region INotifyDataErrorInfo
    public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;

    public bool HasErrors
    {
        get
        {
            return _errorFromProperty.Values.Any(x => x != null);
        }
    }

    public IEnumerable GetErrors(string propertyName)
    {
        if (string.IsNullOrEmpty(propertyName))
            return _errorFromProperty.Values;

        else if (_errorFromProperty.ContainsKey(propertyName))
        {
            if (_errorFromProperty[propertyName] == null)
                return null;
            else
                return new[] { _errorFromProperty[propertyName] };
        }

        else
            return null;
    }

    private Dictionary<string, string> _errorFromProperty;
    #endregion
}

native DataGrid, Telerik:

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Rows}">
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Amount}"/>
    <DataGridComboBoxColumn SelectedItemBinding="{Binding Fee, UpdateSourceTrigger=PropertyChanged}"
                            ItemsSource="{x:Static local:FeeType.List}"
                            DisplayMemberPath="Name"
                            Width="200"/>
  </DataGrid.Columns>
</DataGrid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Rows = new List<RowViewModel>
        {
            new RowViewModel(),
            new RowViewModel(),
        };

        DataContext = this;
    }

    public List<RowViewModel> Rows { get; } 
}

FeeType Min Max , INotifyPropertyChanged , .

"MVVM", "ViewModels", " " .., , WPF, , MVVM. , .

+4

Source: https://habr.com/ru/post/1016214/


All Articles