在WPF中应用MVVM模式一

目录结构:

image

Command/CommandReference.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;

namespace WpfApplication1.Command
{
public class CommandReference : Freezable, ICommand
{
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandReference) new PropertyMetadata(new PropertyChangedCallback(OnCommandChanged)));
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
#region ICommand Members
public bool CanExecute(object parameter)
{
if (Command != null)
return Command.CanExecute(parameter);
return false;
}
public void Execute(object parameter)
{
Command.Execute(parameter);
}
public event EventHandler CanExecuteChanged;
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
CommandReference commandReference
= d as CommandReference;
ICommand oldCommand
= e.OldValue as ICommand;
ICommand newCommand
= e.NewValue as ICommand;
if (oldCommand != null)
{
oldCommand.CanExecuteChanged
-= commandReference.CanExecuteChanged;
}
if (newCommand != null)
{
newCommand.CanExecuteChanged
+= commandReference.CanExecuteChanged;
}
}
#endregion
#region Freezable
protected override Freezable CreateInstanceCore()
{
throw new NotImplementedException();
}
#endregion
}
}

Command/DelegateCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
namespace WpfApplication1.Command
{
public class DelegateCommand : ICommand
{
private Action<object> _action;
public DelegateCommand(Action<object> action)
{
this._action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add { }
remove { }
}
public void Execute(object parameter)
{
this._action(parameter);
}
}
}

Model/DBOP.cs

public class DBOP : IDBOp, IDisposable
{
private static NorthwindDataContext ctx;
public DBOP()
{
ctx
= new NorthwindDataContext();
}
public void Add<T>(T _entity) where T : class
{
var table
= ctx.GetTable<T>();
table.InsertOnSubmit(_entity);
}
public void Delete<T>(T _entity) where T : class
{
var table
= ctx.GetTable<T>();
table.DeleteOnSubmit(_entity);
}
public void Commit()
{
ctx.SubmitChanges();
}
public IList<T> GetAll<T>() where T : class
{
return ctx.GetTable<T>().ToList();
}
public void Update<T>(T _entity) where T : class
{
var table
= ctx.GetTable<T>();
table.Attach(_entity,
true);
}
public T GetById<T>(Func<T, bool> _condition) where T : class
{
return ctx.GetTable<T>().Where(_condition).FirstOrDefault();
}
public void Dispose()
{
throw new NotImplementedException();
}
}

Model/IDBOp.cs

public interface IDBOp
{
#region IDBOp
void Add<T>(T _entity) where T : class;
void Delete<T>(T _entity) where T : class;
void Update<T>(T _entity) where T : class;
void Commit();
IList
<T> GetAll<T>() where T : class;
T GetById
<T>(Func<T, bool> _condition) where T : class;
#endregion
}

Model/LINQHelper.cs

public static class LINQHelper
{
/// <summary>
/// LINQ 扩展
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="collection">linq lanuage</param>
/// <param name="range">linq lanuage</param>
/// <returns>ObservableCollection 泛型</returns>
public static ObservableCollection<T> LinqConvertToObservable<T>(this ObservableCollection<T> collection, IEnumerable<T> range)
{
foreach (var x in range)
{
collection.Add(x);
}
return collection;
}
}

View/Category.xaml

<UserControl x:Class="WpfApplication1.View.Category"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc
="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d
="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm
="clr-namespace:WpfApplication1.ViewModel"
mc:Ignorable
="d"
d:DesignHeight
="300" d:DesignWidth="696">
<Grid Name="Categorys" >
<Grid.RowDefinitions >
<RowDefinition Height="30"/>
<RowDefinition Height="50"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="120" />
<ColumnDefinition Width="100" />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="CategoryName:"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding CategoryName, UpdateSourceTrigger=PropertyChanged}" />
<Label Grid.Row="0" Grid.Column="2" Content="Description:"/>
<TextBox Grid.Row="0" Grid.Column="3" Text="{Binding Description, UpdateSourceTrigger=PropertyChanged}" />
<Button Grid.Row="0" Grid.Column="4" Content="ADD" Command="{Binding AddcatecoryComman}"/>
</Grid>
</UserControl>

View/Category.xaml.cs

public partial class Category : UserControl
{
public Category()
{
InitializeComponent();
this.DataContext = new CategoryViewModel();
}
}

ViewModel/CategoryViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WpfApplication1.Model;
using WpfApplication1.Command;
using System.ComponentModel;
using System.Windows.Input;
namespace WpfApplication1.ViewModel
{
public class CategoryViewModel : INotifyPropertyChanged
{
private IDBOp idbop;
private NorthwindDataContext ctx;
private Category category { get; set; }
private ICommand addcatecoryComman;
public ICommand AddcatecoryComman
{
get
{
if (addcatecoryComman == null)
{
this.addcatecoryComman = new DelegateCommand(this.ExeAddCategory);
}
return addcatecoryComman;
}
}
public CategoryViewModel()
{
idbop
= new DBOP();
ctx
= new NorthwindDataContext();
category
= new Category();
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string _propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(_propertyName));
}
}
public int CategoryID
{
get
{
return category.CategoryID;
}
set
{
if (category.CategoryID != value)
{
category.CategoryID
= value;
OnPropertyChanged(
"CategoryID");
}
}
}
public string CategoryName
{
get
{
return category.CategoryName;
}
set
{
if (category.CategoryName != value)
{
category.CategoryName
= value;
OnPropertyChanged(
"CategoryName");
}
}
}
public string Description
{
get
{
return category.Description;
}
set
{
if (category.Description != value)
{
category.Description
= value;
OnPropertyChanged(
"Description");
}
}
}
private void ExeAddCategory(object sender)
{
Category localmodel
= new Category();
localmodel.CategoryName
= this.CategoryName;
localmodel.Description
= this.Description;
idbop.Add
<Category>(localmodel);
idbop.Commit();
}

}
}

运行效果:
image
image
posted @ 2011-03-07 05:54  Jon.Zhiwei@hotmail.com  Views(673)  Comments(0)    收藏  举报