Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Last active November 26, 2015 15:18
Show Gist options
  • Save BrianJVarley/c0c61c9efdd6520e7713 to your computer and use it in GitHub Desktop.
Save BrianJVarley/c0c61c9efdd6520e7713 to your computer and use it in GitHub Desktop.
using MongoDBApp.DAL;
using MongoDBApp.Models;
using MongoDBApp.Services;
using MongoDBApp.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MongoDBApp.Views
{
/// <summary>
/// Interaction logic for MainView.xaml
/// </summary>
public partial class MainView : Window
{
private MainViewModel ViewModel { get; set; }
private static ICustomerDataService customerDataService = new CustomerDataService(CustomerRepository.Instance);
public MainView()
{
InitializeComponent();
ViewModel = new MainViewModel(customerDataService);
this.DataContext = ViewModel;
}
}
}
<Window x:Class="MongoDBApp.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:email_validator="clr-namespace:MongoDBApp.Validator"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="Orders Dashbord"
Width="800"
Height="500">
<xctk:BusyIndicator IsBusy="{Binding ButtonEnabled}">
<Grid>
<TabControl>
<TabItem Header="Customer">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="70" />
<RowDefinition Height="Auto" />
<RowDefinition Height="1*" />
<RowDefinition Height=".50*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".1*" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<DataGrid x:Name="customersgrid"
Grid.Row="0"
Grid.RowSpan="3"
Grid.Column="1"
Grid.ColumnSpan="3"
AutoGenerateColumns="False"
ItemsSource="{Binding Customers}"
SelectedItem="{Binding SelectedCustomer}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Id}" Header="ID" />
<DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
<DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
<DataGridTextColumn Binding="{Binding Email}" Header="Email" />
</DataGrid.Columns>
</DataGrid>
<Label Grid.Row="4"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Content="First Name:" />
<TextBox x:Name="fNameTbx"
Grid.Row="4"
Grid.Column="2"
Grid.ColumnSpan="2"
Width="120"
Height="23"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{Binding SelectedCustomer.FirstName}"
TextWrapping="Wrap" />
<TextBlock x:Name="iDTbx"
Grid.Row="4"
Grid.Column="2"
Grid.ColumnSpan="2"
Width="180"
Height="23"
HorizontalAlignment="Right"
VerticalAlignment="Top"
Text="{Binding SelectedCustomer.Id}"
TextWrapping="Wrap" />
<Label Grid.Row="4"
Grid.Column="3"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="Customer ID:" />
<Label Grid.Row="5"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Content="Last Name:" />
<TextBox x:Name="lNameTbx"
Grid.Row="5"
Grid.Column="2"
Width="120"
Height="23"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{Binding SelectedCustomer.LastName}"
TextWrapping="Wrap" />
<Label Grid.Row="6"
Grid.Column="1"
HorizontalAlignment="Center"
VerticalAlignment="Top"
Content="Email:" />
<TextBox x:Name="emailTbx"
Grid.Row="6"
Grid.Column="2"
Grid.ColumnSpan="2"
Width="120"
Height="23"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Binding Path="SelectedCustomer.Email" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<email_validator:RegexValidationRule ValidationStep="RawProposedValue" />
</Binding.ValidationRules>
</Binding>
</TextBox>
<Button x:Name="addBtn"
Grid.Row="7"
Grid.Column="1"
Width="75"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding CreateCommand}"
Content="Add" />
<Button x:Name="updateBtn"
Grid.Row="7"
Grid.Column="2"
Width="75"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding UpdateCommand}"
Content="Update">
<Button.ToolTip>
<ToolTip>
<StackPanel>
<TextBlock><Run Text="Updates customer record" />
</TextBlock>
</StackPanel>
</ToolTip>
</Button.ToolTip>
</Button>
<Button x:Name="deleteBtn"
Grid.Row="7"
Grid.Column="3"
Width="75"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding DeleteCommand}"
Content="Delete" />
</Grid>
</TabItem>
<TabItem Header="OrderStatus" />
<TextBlock Width="100"
Height="100"
Text="TextBlock"
TextWrapping="Wrap" />
</TabControl>
</Grid>
</xctk:BusyIndicator>
</Window>
using Microsoft.Practices.Prism.Commands;
using MongoDBApp.Common;
using MongoDBApp.Models;
using MongoDBApp.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using MongoDBApp.Extensions;
using MongoDB.Bson;
using System.Windows.Input;
namespace MongoDBApp.ViewModels
{
class MainViewModel : INotifyPropertyChanged
{
public ICommand UpdateCommand { get; set; }
public ICommand CreateCommand { get; set; }
public ICommand DeleteCommand { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
private ICustomerDataService _customerDataService;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public MainViewModel(ICustomerDataService customerDataService)
{
this._customerDataService = customerDataService;
QueryDataFromPersistence();
UpdateCommand = new CustomCommand((c) => UpdateCustomerAsync(c).FireAndLogErrors(), CanModifyCustomer);
DeleteCommand = new CustomCommand((c) => DeleteCustomerAsync(c).FireAndLogErrors(), CanModifyCustomer);
CreateCommand = new CustomCommand((c) => AddCustomerAsync(c).FireAndLogErrors(), CanModifyCustomer);
}
#region Properties
private MainViewModel selectedModel;
public MainViewModel SelectedModel
{
get
{
return selectedModel;
}
set
{
selectedModel = value;
RaisePropertyChanged("SelectedModel");
}
}
private CustomerModel selectedCustomer;
public CustomerModel SelectedCustomer
{
get
{
return selectedCustomer;
}
set
{
selectedCustomer = value;
RaisePropertyChanged("SelectedCustomer");
}
}
private ObservableCollection<CustomerModel> customers;
public ObservableCollection<CustomerModel> Customers
{
get
{
return customers;
}
set
{
customers = value;
RaisePropertyChanged("Customers");
}
}
private Boolean button_enabled;
public Boolean ButtonEnabled
{
get { return button_enabled; }
set
{
button_enabled = value;
RaisePropertyChanged("ButtonEnabled");
}
}
private ObjectId _id;
public ObjectId ID
{
get
{
return this._id;
}
set
{
this._id = value;
RaisePropertyChanged("ID");
}
}
private string firstName;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
RaisePropertyChanged("FirstName");
}
}
private string lastName;
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
RaisePropertyChanged("LastName");
}
}
private string email;
public string Email
{
get
{
return this.email;
}
set
{
this.email = value;
RaisePropertyChanged("Email");
}
}
#endregion
private bool CanModifyCustomer(object obj)
{
return true;
}
#region persistence methods
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
private async Task UpdateCustomerAsync(object customer) {
ButtonEnabled = true;
await Task.Run(() => _customerDataService.UpdateCustomer(selectedCustomer));
ButtonEnabled = false;
}
private async Task DeleteCustomerAsync(object customer)
{
ButtonEnabled = true;
await Task.Run(() => _customerDataService.DeleteCustomer(selectedCustomer));
ButtonEnabled = false;
}
private async Task AddCustomerAsync(object customer)
{
ButtonEnabled = true;
await Task.Run(() => _customerDataService.AddCustomer(new CustomerModel()));
ButtonEnabled = false;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment