Skip to content

Instantly share code, notes, and snippets.

@bobbychopra
Created June 8, 2012 12:59
Show Gist options
  • Save bobbychopra/2895470 to your computer and use it in GitHub Desktop.
Save bobbychopra/2895470 to your computer and use it in GitHub Desktop.
WPF Delegate Command
using System;
using System.Windows.Input;
namespace MyNameSpace
{
/// <summary>
/// DelegateCommand borrowed from
/// http://www.wpftutorial.net/DelegateCommand.html
/// </summary>
public class DelegateCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
public DelegateCommand(Action<object> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<object> execute,
Predicate<object> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
#region ICommand Members
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if (_canExecute == null)
{
return true;
}
return _canExecute(parameter);
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
<Window x:Class="MyNamespace.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AllowDrop="True" Height="133" Width="441">
<StackPanel>
<TextBox Name="txtFilePath" Text="{Binding FilePath, UpdateSourceTrigger=PropertyChanged}" />
<Button Name="btnUploadFile" Content="Load Tag Data From File" Command="{Binding Upload}" />
</StackPanel>
</Window>
using System.Windows;
using System.Windows.Controls;
namespace MyNameSpace
{
public partial class ShellView : Window
{
public ShellView()
{
InitializeComponent();
this.DataContext = new ShellViewModel();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Windows.Input;
namespace MyNameSpace
{
public class ShellViewModel : INotifyPropertyChanged
{
private string _filePath = @"C:\";
public string FilePath
{
get { return _filePath; }
set
{
_filePath = value;
NotifyOfPropertyChange(() => FilePath);
((DelegateCommand)Upload).RaiseCanExecuteChanged();
}
}
private ICommand _upload;
public ICommand Upload
{
get
{
if (null == _upload)
_upload = new DelegateCommand(LoadFile, CanLoadFile);
return _upload;
}
}
public bool CanLoadFile(object param)
{
return File.Exists(_filePath);
}
public void LoadFile(object param)
{
//Do Stuff
}
#region Property Notification
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyOfPropertyChange<TValue>(Expression<Func<TValue>> propertySelector)
{
if (PropertyChanged != null)
{
var memberExpression = propertySelector.Body as MemberExpression;
if (memberExpression != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(memberExpression.Member.Name));
}
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment