Witam!
Chciałem napisać program, który sprawdza poprawność podanego numeru PESEL, a następnie wyświetla przy pomocy XAML dane, które udało się uzyskać z niego. Na pierwszy ogień chciałem żeby wyświetlił Rok z podanego numeru, jednakże nie mogę tego sprawdzić ponieważ przycisk jest nie aktywny, dlatego też spójrzcie na ten kod i powiedzcie (lub podpowiedzcie) co robię źle.
Ten program pisałem przy użyciu wzroca MVVM.
Model:
class Data
{
public int Year { get; set; }
public int Month { get; set; }
public int Day { get; set; }
public string Sex { get; set; }
public int ControlNumber { get; set; }
public void UpdateProperties(string _string)
{
if (_string.Length == 11)
{
Decoding dekoder = new Decoding();
this.Year = dekoder.Year(_string);
this.Month = dekoder.Month(_string);
this.Day = dekoder.Day(_string);
this.Sex = dekoder.Sex(_string);
this.ControlNumber = dekoder.ControlNumber(_string);
}
}
public bool WhetherYearIsCorrect()
{
return Year < 1900 ? false : true;
}
public bool WhetherControlNumerIsCorrect(string _string)
{
if (_string.Length == 11)
{
int sum = 0, controlNumber = 0;
for (int i = 0; i < _string.Length - 1; i++)
{
if (i == 0 || i == 4 || i == 8) sum += int.Parse(_string[i].ToString()) * 1;
if (i == 1 || i == 5 || i == 9) sum += int.Parse(_string[i].ToString()) * 3;
if (i == 2 || i == 6) sum += int.Parse(_string[i].ToString()) * 7;
if (i == 3 || i == 7) sum += int.Parse(_string[i].ToString()) * 9;
}
controlNumber = 10 - (sum % 10);
return this.ControlNumber == controlNumber ? true : false;
}
return false;
}
public bool WhetherMonthIsCorrect()
{
return Month > 0 && Month < 13 ? true : false;
}
public bool WhetherDayIsCorrect()
{
switch (Month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (Day > 0 && Day < 31) return true;
break;
case 4:
case 6:
case 9:
case 11:
if (Day > 0 && Day < 30) return true;
break;
case 2:
if (Day > 0 && Day < 29) return true;
break;
}
return false;
}
public bool WhetherDataIsCorrect(string _string)
{
UpdateProperties(_string);
return WhetherYearIsCorrect() && WhetherControlNumerIsCorrect(_string) && WhetherMonthIsCorrect() && WhetherDayIsCorrect();
}
}
Model Widoku:
class ViewModel : INotifyPropertyChanged
{
public Data data = new Data();
public int Year
{
get
{
return data.Year;
}
}
public int Month
{
get
{
return data.Month;
}
}
public int Day
{
get
{
return data.Day;
}
}
public string Sex
{
get
{
return data.Sex;
}
}
public int ControlNumber
{
get
{
return data.ControlNumber;
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public bool WhetherStringIsCorrect(string _string)
{
if (string.IsNullOrWhiteSpace(_string)) return false;
if (_string.Length != 11) return false;
int liczba;
if (!int.TryParse(_string, out liczba)) return false;
else return data.WhetherDataIsCorrect(_string);
}
private ICommand changeCommand;
public void Change(object argument)
{
string _string = argument.ToString();
if (WhetherStringIsCorrect(_string))
{
data.UpdateProperties(_string);
OnPropertyChanged("Year");
OnPropertyChanged("Month");
OnPropertyChanged("Day");
OnPropertyChanged("Sex");
OnPropertyChanged("ControlNumber");
}
}
public ICommand ChangeCommand
{
get
{
if (changeCommand == null)
{
changeCommand = new RelayCommand(
(object argument) =>
{
try
{
Change(argument);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "PESEL", MessageBoxButton.OK, MessageBoxImage.Error);
}
},
(object argument) =>
{
return WhetherStringIsCorrect((string)argument);
}
);
}
return changeCommand;
}
}
}
class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion
#region Constructor
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
if (execute == null) throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
//Metoda CanExecute sprawdza czy wykonianie polecenia jest możliwe
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add
{
if (_canExecute != null) CommandManager.RequerySuggested += value;
}
remove
{
if (_canExecute != null) CommandManager.RequerySuggested -= value;
}
}
//Metoda Execute wykonuje zasadnicze polecenie
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion
}
Konwerter:
public class BoolToBrushConverter : IValueConverter
{
public Brush ColorTrue { get; set; }
public Brush ColorFalse { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool b = (bool)value;
return b ? ColorTrue : ColorFalse;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Widok:
<Window x:Class="PESEL.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Model="clr-namespace:PESEL.Model"
xmlns:ViewModel="clr-namespace:PESEL.ViewModel"
xmlns:Converter="clr-namespace:PESEL.Converters"
Title="PESEL (c) 2015" Height="300" Width="300" ResizeMode="NoResize">
<Window.Resources>
<Converter:BoolToBrushConverter x:Key="boolToBrush" KolorFalse="Red" KolorTrue="Black"/>
</Window.Resources>
<Window.DataContext>
<ViewModel:ViewModel />
</Window.DataContext>
<StackPanel>
<!--Wyświetlane po uruchomieniu programu!-->
<StackPanel Height="42" Orientation="Horizontal" Margin="10">
<TextBlock Margin="10" Width="110" FontSize="16" Text="Podaj PESEL:" />
<TextBox Width="120" Margin="10" TextAlignment="Center" FontSize="14" MaxLength="11"
Foreground="{Binding ElementName=Walidacja, Path=IsEnabled, Mode=OneWay, Converter={StaticResource boolToBrush}}" />
</StackPanel>
<Button x:Name="Walidacja" Content="SPRAWDŹ" Margin="100,10,100,10"
Command="{Binding ZmieńCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=Rok, Path=Text}"/>
<StackPanel Margin="10">
<!--ROK URODZENIA-->
<StackPanel Height="35" Orientation="Horizontal" Margin="10,0">
<TextBlock Margin="10" Width="110" Height="20"
TextWrapping="Wrap" Text="Rok Urodzenia:" />
<TextBlock x:Name="Rok" Margin="10" Width="80" Height="20" Text="{Binding Rok, Mode=OneWay}"/>
</StackPanel>
</StackPanel>
</StackPanel>
</Window>