RangeAttribute nie dziala

0

Mam taki kod. Gdy do NumberOfPeriods zostaje przypisane -1 to wtedy normalnie ReportConfig.NumberOfPeriods ma -1. O czym zapomnialem?

[Range(1, Int32.MaxValue, ErrorMessage = "Period recurrence should be positive.")]
public int NumberOfPeriods
{
    get { return ReportConfig.NumberOfPeriods; }
    set
    {
        ReportConfig.NumberOfPeriods = value;
        OnPropertyChanged();
    }
} 
0

Ale masz problem z tym że gdzieś w kodzie masz

NumberOfPeriods = -1;

i działa a według Ciebie nie powinno?
Czy masz problem że w UI jak wpiszesz w np. TextBox: -1, to przechodzi? :>

0

@DibbyDum Mam problem z UI ze jak wpisze -1 to przechodzi

1

Nie wiem czy to idealne rozwiązanie bo w WPF od czasów studiów nic nie pisałem ale przykład:
WPF:

<Grid>
   <TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding NumberOfPeriods, Mode=TwoWay, 
                     ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Top" Width="120"/>
</Grid>

C#:

public partial class MainWindow : Window
{
   public MainWindow()
   {
      InitializeComponent();
      DataContext = new Foo();
   }
}

public class Foo : INotifyPropertyChanged
{
   private int _numberOfPeriods;

   [Range(1, int.MaxValue, ErrorMessage = @"Period recurrence should be positive.")]
   public int NumberOfPeriods
   {
      get { return _numberOfPeriods; }
      set
      {
         if (value == _numberOfPeriods) return;

         Validator.ValidateProperty(value,
            new ValidationContext(this, null, null) {MemberName = "NumberOfPeriods"});

         _numberOfPeriods = value;
         OnPropertyChanged();
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   [NotifyPropertyChangedInvocator]
   protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      var handler = PropertyChanged;
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
   }
}
0

dziala dziekowac :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1