Created
October 13, 2014 08:15
-
-
Save kevingoos/2de25ae7ff326065d269 to your computer and use it in GitHub Desktop.
WPF and MVVM example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public MustInherit Class BaseViewModel | |
Implements INotifyPropertyChanged | |
Protected Sub RaisePropertyChanged(propertyName As String) | |
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) | |
End Sub | |
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged | |
End Class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class CategoryModel | |
Inherits Model | |
Private _id As Guid | |
Private _categoryName As String | |
Private _categoryType As Boolean | |
Public Sub New(ByVal categoryName As String, ByVal categoryType As Boolean) | |
Me.CategoryName = categoryName | |
Me.CategoryType = categoryType | |
End Sub | |
Public Sub New(ByVal id As Guid, ByVal categoryName As String, ByVal categoryType As Boolean) | |
Me.Id = id | |
Me.CategoryName = categoryName | |
Me.CategoryType = categoryType | |
End Sub | |
Public Property Id() As Guid | |
Get | |
Return _id | |
End Get | |
Private Set(value As Guid) | |
_id = value | |
End Set | |
End Property | |
Public Property CategoryName() As String | |
Get | |
Return _categoryName | |
End Get | |
Set(value As String) | |
_categoryName = value | |
RaisePropertyChanged("CategoryName") | |
End Set | |
End Property | |
'inkomen = true , uitgave = false | |
Public Property CategoryType() As Boolean | |
Get | |
Return _categoryType | |
End Get | |
Set(value As Boolean) | |
_categoryType = value | |
RaisePropertyChanged("CategoryType") | |
End Set | |
End Property | |
Public Function ToCategory() As Category | |
Dim categorie As Category = New Category() | |
categorie.Id = _id | |
categorie.CategoryName = CategoryName | |
categorie.CategoryType = CategoryType | |
Return categorie | |
End Function | |
End Class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class CategoryViewModel | |
Inherits BaseViewModel | |
Private _categories As ObservableCollection(Of CategoryModel) | |
Private ReadOnly _unitOfWork As UnitOfWork | |
Public Sub New() | |
_unitOfWork = New UnitOfWork() | |
End Sub | |
Public Sub Init() | |
Dim obscategories = _unitOfWork.GetCategoryRepository().GetAll() | |
Dim observable = New ObservableCollection(Of CategoryModel)() | |
For Each c As Category In obscategories | |
observable.Add(New CategoryModel(c.CategoryName, c.CategoryType)) | |
Next | |
Categories = observable | |
End Sub | |
Public Property Categories As ObservableCollection(Of CategoryModel) | |
Get | |
Return _categories | |
End Get | |
Set(value As ObservableCollection(Of CategoryModel)) | |
_categories = value | |
RaisePropertyChanged("Category") | |
End Set | |
End Property | |
Public Sub AddCategory(categoryName As String, categoryType As Boolean) | |
Dim cat = New CategoryModel(categoryName:=categoryName, categoryType:=categoryType) | |
_categories.Add(cat) | |
_unitOfWork.CategoryRepository.Add(cat.ToCategory()) | |
_unitOfWork.Commit() | |
End Sub | |
Public Sub RemoveCategory(removedIndex As Integer) | |
Dim cat = _categories.ElementAt(removedIndex) | |
Dim entity = _unitOfWork.CategoryRepository.GetById(cat.Id) | |
_unitOfWork.CategoryRepository.Delete(entity) | |
_categories.RemoveAt(removedIndex) | |
_unitOfWork.Commit() | |
End Sub | |
End Class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:vm="clr-namespace:MoneyManager.WPF.ViewModels" | |
WindowStartupLocation="CenterScreen" | |
Title="MainWindow" Height="500" Width="300"> | |
<Window.Resources> | |
<DataTemplate x:Key="CategoryModelTemplate"> | |
<StackPanel> | |
<TextBlock Text="{Binding CategoryName}" /> | |
<TextBlock Text="{Binding CategoryType}" FontSize="7"/> | |
</StackPanel> | |
</DataTemplate> | |
</Window.Resources> | |
<Grid Margin="0,60"> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition/> | |
<ColumnDefinition/> | |
</Grid.ColumnDefinitions> | |
<Grid.RowDefinitions> | |
<RowDefinition/> | |
<RowDefinition/> | |
<RowDefinition/> | |
<RowDefinition/> | |
<RowDefinition/> | |
</Grid.RowDefinitions> | |
<TextBlock Grid.Column="0" Grid.Row="0" Text="Categories" FontWeight="Bold" VerticalAlignment="Center" Margin="20,0"/> | |
<ListBox Name="CategoriesListBox" Grid.Column="0" Grid.Row="1" ItemsSource="{Binding Categories}" ItemTemplate="{DynamicResource CategoryModelTemplate}" HorizontalAlignment="left" Margin="20,0,0,0" Grid.RowSpan="4" Width="150" /> | |
<Button Content="Add " HorizontalAlignment="Left" Height="22" Grid.Row="1" VerticalAlignment="Top" Width="129.333" FontSize="9.333" Grid.Column="1" Margin="0,18,0,0"/> | |
<Button Content="Delete selected" HorizontalAlignment="Left" Height="22" Grid.Row="4" VerticalAlignment="Top" Width="129.333" FontSize="9.333" Grid.Column="1" Margin="0,18,0,0"/> | |
<TextBox Name="CategoryField" HorizontalAlignment="Left" Height="22" Grid.Row="2" TextWrapping="Wrap" Text="Category" VerticalAlignment="Top" Width="129.333" FontWeight="ExtraLight" Grid.Column="1" FontSize="9.333"/> | |
<RadioButton Name="CategoryType" Content="Income" HorizontalAlignment="Left" Height="16" Margin="0,24,0,0" Grid.Row="2" VerticalAlignment="Top" Width="71.322" FontSize="9.333" Grid.Column="1" GroupName="CategoryType" IsChecked="True"/> | |
<RadioButton Content="Expense" HorizontalAlignment="Left" Height="16" Margin="76.322,24,0,0" Grid.Row="2" VerticalAlignment="Top" Width="71.322" Grid.Column="1" FontSize="9.333" GroupName="CategoryType"/> | |
</Grid> | |
</Window> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class MainWindow | |
Public Sub New() | |
' This call is required by the designer. | |
InitializeComponent() | |
' Add any initialization after the InitializeComponent() call. | |
Me.DataContext = New CategoryViewModel() | |
End Sub | |
End Class |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class Model | |
Implements INotifyPropertyChanged | |
Protected Sub RaisePropertyChanged(propertyName As String) | |
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName)) | |
End Sub | |
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment