Created
September 1, 2014 11:35
-
-
Save anonymous/9efb10278e6921c28975 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 WpfApplication3 | |
{ | |
using System; | |
using System.Windows.Input; | |
/// <summary> | |
/// Simple command that allows us to use delegates to control our actions | |
/// </summary> | |
public class ActionCommand : ICommand | |
{ | |
/// <summary> | |
/// Action to execute when this command is run | |
/// </summary> | |
private readonly Action execute; | |
/// <summary> | |
/// The action to execute when a param is passed | |
/// </summary> | |
private readonly Action<object> executeParam; | |
/// <summary> | |
/// Func to determine whether we can execute or not | |
/// </summary> | |
private readonly Func<bool> canExecute; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ActionCommand"/> class. | |
/// </summary> | |
/// <param name="executeAction">The action to execute when the command is executed.</param> | |
/// <param name="canExecuteFunc">A predicate to determine whether we can run.</param> | |
public ActionCommand(Action executeAction, Func<bool> canExecuteFunc) | |
{ | |
this.execute = executeAction; | |
this.canExecute = canExecuteFunc; | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ActionCommand"/> class. | |
/// </summary> | |
/// <param name="executeAction">The action to execute when the command is executed.</param> | |
/// <param name="canExecuteFunc">A predicate to determine whether we can run.</param> | |
public ActionCommand(Action<object> executeAction, Func<bool> canExecuteFunc) | |
{ | |
this.executeParam = executeAction; | |
this.canExecute = canExecuteFunc; | |
} | |
/// <summary> | |
/// Occurs when changes occur that affect whether or not the command should execute. | |
/// </summary> | |
event EventHandler ICommand.CanExecuteChanged | |
{ | |
add { CommandManager.RequerySuggested += value; } | |
remove { CommandManager.RequerySuggested -= value; } | |
} | |
/// <summary> | |
/// Defines the method that determines whether the command can execute in its current state. | |
/// </summary> | |
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> | |
/// <returns> | |
/// true if this command can be executed; otherwise, false. | |
/// </returns> | |
bool ICommand.CanExecute(object parameter) | |
{ | |
if (this.canExecute != null) | |
{ | |
return this.canExecute(); | |
} | |
return true; | |
} | |
/// <summary> | |
/// Defines the method to be called when the command is invoked. | |
/// </summary> | |
/// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> | |
void ICommand.Execute(object parameter) | |
{ | |
if (this.execute != null) | |
{ | |
this.execute(); | |
} | |
else if (this.executeParam != null) | |
{ | |
this.executeParam(parameter); | |
} | |
} | |
} | |
} |
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="WpfApplication3.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="clr-namespace:WpfApplication3" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.DataContext> | |
<local:ViewModel/> | |
</Window.DataContext> | |
<Grid> | |
<Menu VerticalAlignment="Top"> | |
<MenuItem Header="Woo" ItemsSource="{Binding Items}"> | |
<MenuItem.ItemContainerStyle> | |
<Style TargetType="MenuItem"> | |
<Setter Property="Command" Value="{Binding AddItemCommand}"/> | |
</Style> | |
</MenuItem.ItemContainerStyle> | |
</MenuItem> | |
</Menu> | |
<Button Content="Add item" Command="{Binding AddItemCommand}" VerticalAlignment="Bottom"/> | |
</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 System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Windows.Input; | |
namespace WpfApplication3 | |
{ | |
class ViewModel : INotifyPropertyChanged | |
{ | |
private ObservableCollection<string> _items = new ObservableCollection<string>(); | |
public ObservableCollection<string> Items { get { return _items; } set { _items = value; onPropertyChanged(this, "Items"); } } | |
public ViewModel() | |
{ | |
Items.Add("1"); | |
Items.Add("2"); | |
Items.Add("3"); | |
Items.Add("4"); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
private void onPropertyChanged(object sender, string propertyName) | |
{ | |
if (this.PropertyChanged != null) | |
PropertyChanged(sender, new PropertyChangedEventArgs(propertyName)); | |
} | |
private ICommand _addItemCommand; | |
public ICommand AddItemCommand | |
{ | |
get | |
{ | |
if (_addItemCommand == null) | |
{ | |
_addItemCommand = new ActionCommand(param => this.AddItem(), null); | |
} | |
return _addItemCommand; | |
} | |
} | |
private void AddItem() | |
{ | |
Items.Add("777"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment