I am still making experiences with Commands and RoutedEvents. Without using RoutedCommands, I try to realize a ver simple program.
我仍然在使用命令和命令进行体验。不使用RoutedCommands,我试图实现一个非常简单的程序。
Here is my Command class:
这是我的命令类:
public class ColorChanger : ICommand
{
public static readonly RoutedEvent ChangeMyColor = EventManager.RegisterRoutedEvent("ChangeMyColor", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(ColorChanger));
public void Execute(object parameter)
{
RoutedEventArgs eventArgs = new RoutedEventArgs(ChangeMyColor);
Keyboard.FocusedElement.RaiseEvent(eventArgs);
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public static void AddChangeMyColorHandler(DependencyObject o, RoutedEventHandler handler)
{
((UIElement)o).AddHandler(ColorChanger.ChangeMyColor, handler);
}
public static void RemoveChangeMyColorHandler(DependencyObject o, RoutedEventHandler handler)
{
((UIElement)o).AddHandler(ColorChanger.ChangeMyColor, handler);
}
}
To make sure I have a static access to that command, I made a static class for holding all commands:
为了确保对该命令有一个静态访问,我创建了一个静态类,用于保存所有命令:
public static class AppCommands
{
private static ColorChanger colorChanger = new ColorChanger();
public static ColorChanger ColorChanger
{
get { return colorChanger; }
}
}
This is what you will find in my MainWindow.xaml:
这就是你在我的主窗上看到的。
<StackPanel>
<Menu>
<MenuItem Command="{x:Static local:AppCommands.ColorChanger}" Header="ClickMe"
CommandTarget="{Binding ElementName=mainTextBox}" x:Name="menue1"/>
</Menu>
<TextBox Name="mainTextBox"/>
</StackPanel>
What I want is that by clicking the menue1-item the background of the 'mainTextBox' changes. So let's have a look inside my MainWindow.cs:
我想要的是,通过单击menue1项,“mainTextBox”更改的背景。让我们看一下我的主窗口。
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddHandler(ColorChanger.ChangeMyColor,new RoutedEventHandler(test));
}
public void test(object sender, RoutedEventArgs args)
{
Control someCtl = (Control) args.OriginalSource;
someCtl.Background = Brushes.BlueViolet;
}
}
The programm is working - but not correct :) It always changes the background of the MainWindow, but not of my CommandTarget.
这个程序是有效的,但不正确:它总是改变主窗口的背景,而不是我的命令目标。
So - what am I doing wrong? Did I forget something?
我做错了什么?我忘记了什么东西吗?
1 个解决方案
#1
0
OriginalSource
is the reporting source of an event based on pure hit testing. See: http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx.
源是基于纯粹命中测试的事件的报告源。参见:http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx。
In this case it returns your window because it doesn't resolve to any of child elements in its visual tree.
在这种情况下,它返回您的窗口,因为它不解析它的可视树中的任何子元素。
To access your CommandTarget you should use args.Source
.
要访问CommandTarget,您应该使用args.Source。
#1
0
OriginalSource
is the reporting source of an event based on pure hit testing. See: http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx.
源是基于纯粹命中测试的事件的报告源。参见:http://msdn.microsoft.com/en-us/library/system.windows.routedeventargs.originalsource.aspx。
In this case it returns your window because it doesn't resolve to any of child elements in its visual tree.
在这种情况下,它返回您的窗口,因为它不解析它的可视树中的任何子元素。
To access your CommandTarget you should use args.Source
.
要访问CommandTarget,您应该使用args.Source。