8,735
社区成员
发帖
与我相关
我的任务
分享
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
<Grid>
<Grid.Resources>
<local:HelloWorldCommand x:Key="hwc"/>
</Grid.Resources>
<Grid.InputBindings>
<KeyBinding Gesture="Control+H" Command="{StaticResource hwc}" CommandParameter="Hello, world"/>
</Grid.InputBindings>
public static RoutedCommand HelloWorldRoutedCommand = new RoutedCommand()
{
InputGestures = { new KeyGesture(Key.H, ModifierKeys.Control) }
};
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public interface ICommand
{
void Execute(object parameter);
bool CanExecute(object parameter);
event EventHandler CanExecuteChanged;
}
public class HelloWorldCommand : ICommand
{
public void Execute(object parameter)
{
Debug.WriteLine("Hello, world");
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
}
new HelloWorldCommand().Execute(null);
public class HelloWorldCommand : ICommand
{
public void Execute(object parameter)
{
Debug.WriteLine(parameter);
}
public bool CanExecute(object parameter)
{
return parameter != null;
}
public event EventHandler CanExecuteChanged;
}
var hwc = new HelloWorldCommand();
if (hwc.CanExecute(this))
hwc.Execute(this);
<Grid>
<Grid.Resources>
<local:HelloWorldCommand x:Key="hwc"/>
</Grid.Resources>
<Button Command="{StaticResource hwc}">
Click
</Button>
</Grid>
<Grid>
<Grid.Resources>
<local:HelloWorldCommand x:Key="hwc"/>
</Grid.Resources>
<Button CommandParameter="Hello, world" Command="{StaticResource hwc}" >
Click
</Button>
</Grid>
public class SimpleDelegateCommand : ICommand
{
Action<object> _executeDelegate;
public SimpleDelegateCommand(Action<object> executeDelegate)
{
_executeDelegate = executeDelegate;
}
public void Execute(object parameter)
{
_executeDelegate(parameter);
}
public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
}
public class DebugWriter
{
ICommand _indentCommand = new SimpleDelegateCommand( (x) => Debug.Indent() );
ICommand _unindentCommand = new SimpleDelegateCommand( (x) => Debug.Unindent() );
ICommand _writeLineCommand = new SimpleDelegateCommand( (x) => Debug.WriteLine(x) );
public ICommand IndentCommand { get { return _indentCommand; } }
public ICommand UnindentCommand { get { return _unindentCommand; } }
public ICommand WriteLineCommand { get { return _writeLineCommand; } }
public int IndentSize
{
get { return Debug.IndentSize; }
set { Debug.IndentSize = value; }
}
}
<StackPanel>
<StackPanel.DataContext>
<local:DebugWriter />
</StackPanel.DataContext>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/><ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/><RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Margin="3">Indent size:</TextBlock>
<TextBlock Margin="4" Grid.Column="1" Text="{Binding IndentSize}" />
<TextBlock Grid.Row="1" Margin="3">Output string:</TextBlock>
<TextBox Text="s" Grid.Row="1" Grid.Column="1" Name="OutputString" />
</Grid>
<Button Command="{Binding IndentCommand}">Indent</Button>
<Button Command="{Binding UnindentCommand}">Unindent</Button>
<Button CommandParameter="{Binding Text,ElementName=OutputString}"
Command="{Binding WriteLineCommand}">WriteLine</Button>
</StackPanel>
public static RoutedCommand HelloWorldRoutedCommand = new RoutedCommand();
<Window.CommandBindings>
<CommandBinding
Command="{x:Static local:Window1.HelloWorldRoutedCommand}"
Executed="CommandBinding_Executed" />
</Window.CommandBindings>
<Grid>
...
<Button Command="{x:Static local:Window1.HelloWorldRoutedCommand}">Hello, world </Button>
...
</Grid>
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
if( e.Source is Button )
Debug.WriteLine((e.Source as Button).Content);
else
Debug.WriteLine("Hello, world");
}
class MyCommand : ICommand
{
public bool CanExecute(object parameter)
{
return maybeTrueOrFalse;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
// Do something awesome.
}
}