public class BaseViewModel : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = "") | |
{ | |
if (EqualityComparer<T>.Default.Equals(storage, value)) | |
return false; | |
storage = value; | |
this.OnPropertyChanged(propertyName); | |
return true; | |
} | |
} |
public static class DataStorage | |
{ | |
private static readonly Technical_BestBeforeDates_Model[] data = | |
new Technical_BestBeforeDates_Model[] | |
{ | |
new Technical_BestBeforeDates_Model(0, "12", "desc1"), | |
new Technical_BestBeforeDates_Model(1, "12", "desc2"), | |
new Technical_BestBeforeDates_Model(2, "13", "desc3"), | |
new Technical_BestBeforeDates_Model(3, "14", "desc2"), | |
}; | |
private static bool Filter | |
(Technical_BestBeforeDates_Model tbm, string code, string description) => | |
(string.IsNullOrEmpty(code) || tbm.ProductCode == code) | |
&& (string.IsNullOrEmpty(description) || tbm.ProductDescription.Contains(description)); | |
public static IEnumerable<Technical_BestBeforeDates_Model> GetFilteredData | |
(string code, string description) => | |
data.Where(tbm => Filter(tbm, code, description)); | |
} |
public class RelayCommand : ICommand | |
{ | |
private Action<object> execute; | |
private Func<object, bool> canExecute; | |
public event EventHandler CanExecuteChanged | |
{ | |
add { CommandManager.RequerySuggested += value; } | |
remove { CommandManager.RequerySuggested -= value; } | |
} | |
public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null) | |
{ | |
this.execute = execute; | |
this.canExecute = canExecute; | |
} | |
public bool CanExecute(object parameter) | |
{ | |
return this.canExecute == null || this.canExecute(parameter); | |
} | |
public void Execute(object parameter) | |
{ | |
this.execute(parameter); | |
} | |
} |
public class Technical_BestBeforeDates_Model | |
{ | |
public Technical_BestBeforeDates_Model(int id, string productCode, string productDescription) | |
{ | |
Id = id; | |
ProductCode = productCode; | |
ProductDescription = productDescription; | |
} | |
public int Id { get; private set; } | |
public string ProductCode { get; private set; } | |
public string ProductDescription { get; private set; } | |
} |
public class Technical_BestBeforeDates_Record_ViewModel : BaseViewModel | |
{ | |
private string productCode; | |
private string productDescription; | |
public string ProductCode | |
{ | |
get => productCode; | |
set => SetProperty(ref productCode, value); | |
} | |
public string ProductDescription | |
{ | |
get => productDescription; | |
set => SetProperty(ref productDescription, value); | |
} | |
public ObservableCollection<Technical_BestBeforeDates_Model> VMs { get; } | |
public Technical_BestBeforeDates_Record_ViewModel() | |
{ | |
VMs = new ObservableCollection<Technical_BestBeforeDates_Model>(); | |
FilterCommand = new RelayCommand(_ => UpdateDataFromDatabase()); | |
UpdateDataFromDatabase(); | |
} | |
public void UpdateDataFromDatabase() | |
{ | |
VMs.Clear(); | |
// dummies, replace with call to a DB | |
var records = DataStorage.GetFilteredData(ProductCode, ProductDescription); | |
foreach (Technical_BestBeforeDates_Model model in records) | |
{ | |
VMs.Add(model); | |
} | |
} | |
public ICommand FilterCommand { get; } | |
} |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition Height="*" /> | |
</Grid.RowDefinitions> | |
<Grid> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition Height="Auto" /> | |
</Grid.RowDefinitions> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition Width="*" /> | |
<ColumnDefinition Width="*" /> | |
</Grid.ColumnDefinitions> | |
<TextBlock | |
Grid.Row="0" | |
Grid.Column="0" | |
Margin="5" | |
HorizontalAlignment="Center" | |
Text="Code" /> | |
<TextBlock | |
Grid.Row="1" | |
Grid.Column="0" | |
Margin="5" | |
HorizontalAlignment="Center" | |
Text="Description" /> | |
<TextBox | |
Grid.Row="0" | |
Grid.Column="1" | |
Margin="5" | |
Text="{Binding ProductCode}" /> | |
<TextBox | |
Grid.Row="1" | |
Grid.Column="1" | |
Margin="5" | |
Text="{Binding ProductDescription}" /> | |
<Button | |
Grid.Row="2" | |
Grid.Column="1" | |
MaxWidth="100" | |
Margin="5" | |
HorizontalAlignment="Right" | |
Command="{Binding FilterCommand}" | |
Content="Filter" /> | |
</Grid> | |
<DataGrid | |
Grid.Row="1" | |
AutoGenerateColumns="False" | |
ColumnWidth="auto" | |
IsReadOnly="True" | |
ItemsSource="{Binding VMs}"> | |
<DataGrid.Columns> | |
<DataGridTextColumn Binding="{Binding Id}" Header="Id" /> | |
<DataGridTextColumn Binding="{Binding ProductCode}" Header="Product Code" /> | |
<DataGridTextColumn Binding="{Binding ProductDescription}" Header="Product Description" /> | |
</DataGrid.Columns> | |
</DataGrid> | |
</Grid> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment