Wpf data sync problem

Something is wrong with my binding. But I can not find it

I have a state type control (UserControl) that has a binding-related ItemsControl that relies on a ViewModelBase that provides a BrokenRules list, for example:

<ItemsControl ItemsSource="{Binding BrokenRules}" >
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Hyperlink Foreground="Red" >
                    <TextBlock Text="{Binding Description}" />
                </Hyperlink>
            </TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Binding works the way I want, in the sense that all and all broken rules are displayed. A rule is pretty much just a description and a delegate that runs when the rule is told to test itself.

Most of the rules have descriptions that are known ahead, before the rule is asked to confirm itself. For example, “Name is not rated” is a great description of what went wrong if the delegate is a validator! Name.IsNullOrEmptyAfterTrim () fails.

, . , , , . , .

unit test , . , - , .

, . - , /?

Cheers,
Berryl

=====================

ViewModelBase:

private readonly List<RuleBase> _rules = new List<RuleBase>();

// inheritors add rules as part of construction
protected void _AddRule(RuleBase rule) { _rules.Add(rule); }

public ObservableCollection<RuleBase> BrokenRules { get { return _brokenRules; } }
protected ObservableCollection<RuleBase> _brokenRules;

public virtual IEnumerable<RuleBase> GetBrokenRules()         {
        return GetBrokenRules(string.Empty);
}

public virtual IEnumerable<RuleBase> GetBrokenRules(string property)        {
    property = property.CleanString();

     _brokenRules = new ObservableCollection<RuleBase>();
    foreach (var r in _rules)            {
        // Ensure we only validate this rule 
        if (r.PropertyName != property && property != string.Empty) continue;

        var isRuleBroken = !r.ValidateRule(this);

        if (isRuleBroken) _brokenRules.Add(r);

        return _brokenRules;
    }
+3
1

, BrokenRules , Model View :

public ObservableCollection<BrokenRule> BrokenRules
{
  get;
  set;
}

private void ValidateRules()
{
  // Validation code
  if (!rule.IsValid)
  {
    this.BrokenRules.Add(new BrokenRule { Description = "Duplicated name found" });
  }
}

, , - :

this.BrokenRules = this.ValidateRules();

, ItemsControl, , .

+1

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


All Articles