Last active
August 29, 2015 14:01
-
-
Save kurushimiamai/5a1d8b5717ce6e7b3cbe to your computer and use it in GitHub Desktop.
This file contains 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
namespace Example | |
{ | |
public class Entity | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string Address { get; set; } | |
} | |
} |
This file contains 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
using GalaSoft.MvvmLight; | |
namespace Example | |
{ | |
public class EntityViewModel : ViewModelBase | |
{ | |
public EntityViewModel(Entity e) | |
{ | |
this.entity = e; | |
} | |
Entity entity; | |
public int Id | |
{ | |
get { return entity.Id; } | |
set | |
{ | |
entity.Id = value; | |
RaisePropertyChanged("Id"); | |
} | |
} | |
public string Name | |
{ | |
get { return entity.Name; } | |
set | |
{ | |
entity.Name = value; | |
RaisePropertyChanged("Name"); | |
} | |
} | |
public string Address | |
{ | |
get { return entity.Address; } | |
set | |
{ | |
entity.Address = value; | |
RaisePropertyChanged("Address"); | |
} | |
} | |
} | |
} |
This file contains 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="Example.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:l="clr-namespace:Example" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.Resources> | |
<l:MainWindowViewModel x:Key="vm"/> | |
</Window.Resources> | |
<Grid DataContext="{StaticResource vm}"> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition/> | |
<ColumnDefinition/> | |
<ColumnDefinition/> | |
</Grid.ColumnDefinitions> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="22"/> | |
<RowDefinition Height="22"/> | |
<RowDefinition Height="22"/> | |
<RowDefinition/> | |
</Grid.RowDefinitions> | |
<Label Padding="0,2,0,0" Content="Id" Grid.Column="0" Grid.Row="0"/> | |
<Label Padding="0,2,0,0" Content="Name" Grid.Column="1" Grid.Row="0"/> | |
<Label Padding="0,2,0,0" Content="Address" Grid.Column="2" Grid.Row="0"/> | |
<TextBox Text="{Binding NewEntity.Id}" Grid.Column="0" Grid.Row="1"/> | |
<TextBox Text="{Binding NewEntity.Name}" Grid.Column="1" Grid.Row="1"/> | |
<TextBox Text="{Binding NewEntity.Address}" Grid.Column="2" Grid.Row="1"/> | |
<Button Content="Add" Command="{Binding AddEntityCommand}" Grid.Column="1" Grid.Row="2"/> | |
<DataGrid CanUserAddRows="False" AutoGenerateColumns="False" HeadersVisibility="None" ItemsSource="{Binding Entities}" Grid.ColumnSpan="3" Grid.Row="4"> | |
<DataGrid.Columns> | |
<DataGridTemplateColumn Width="*"> | |
<DataGridTemplateColumn.CellTemplate> | |
<DataTemplate> | |
<Label Content="{Binding Id}"/> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellTemplate> | |
</DataGridTemplateColumn> | |
<DataGridTemplateColumn Width="*"> | |
<DataGridTemplateColumn.CellTemplate> | |
<DataTemplate> | |
<Label Content="{Binding Name}"/> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellTemplate> | |
</DataGridTemplateColumn> | |
<DataGridTemplateColumn Width="*"> | |
<DataGridTemplateColumn.CellTemplate> | |
<DataTemplate> | |
<Label Content="{Binding Address}"/> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellTemplate> | |
</DataGridTemplateColumn> | |
</DataGrid.Columns> | |
</DataGrid> | |
</Grid> | |
</Window> |
This file contains 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
using GalaSoft.MvvmLight; | |
using GalaSoft.MvvmLight.Command; | |
using System.Collections.ObjectModel; | |
namespace Example | |
{ | |
public class MainWindowViewModel : ViewModelBase | |
{ | |
public MainWindowViewModel() | |
{ | |
Entities.Add(new EntityViewModel(new Entity() { Id = 1, Name = "Jeniffer Glasgow", Address = "3, Main Road" })); | |
} | |
/// <summary> | |
/// The <see cref="NewEntity" /> property's name. | |
/// </summary> | |
public const string NewEntityPropertyName = "NewEntity"; | |
private EntityViewModel ne = new EntityViewModel(new Entity()); | |
/// <summary> | |
/// Sets and gets the NewEntity property. | |
/// Changes to that property's value raise the PropertyChanged event. | |
/// </summary> | |
public EntityViewModel NewEntity | |
{ | |
get | |
{ | |
return ne; | |
} | |
set | |
{ | |
if (ne == value) | |
{ | |
return; | |
} | |
RaisePropertyChanging(NewEntityPropertyName); | |
ne = value; | |
RaisePropertyChanged(NewEntityPropertyName); | |
} | |
} | |
private ObservableCollection<EntityViewModel> enitites = new ObservableCollection<EntityViewModel>(); | |
/// <summary> | |
/// Gets the Entities property. | |
/// </summary> | |
public ObservableCollection<EntityViewModel> Entities | |
{ | |
get | |
{ | |
return enitites; | |
} | |
} | |
private RelayCommand addcommand; | |
/// <summary> | |
/// Gets the AddEntityCommand. | |
/// </summary> | |
public RelayCommand AddEntityCommand | |
{ | |
get | |
{ | |
return addcommand | |
?? (addcommand = new RelayCommand( | |
() => | |
{ | |
Entities.Add(NewEntity); | |
NewEntity = new EntityViewModel(new Entity()); | |
})); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment