Hi Is there any way to choose from where XAML should use command bindings event handlers? I added copule of command binding to my cusotm control, however functions which are resonsible for execute and can_execute are not directly in code behind but in another class. This class is derived from Canvas and I create instance of this class in XAML.
Hi,有什么方法可以选择XAML应该使用命令绑定事件处理程序吗?我添加了对我的cusotm控件的命令绑定,但是对于执行和can_execute的功能来说,它们并不是直接在代码后面,而是在另一个类中。这个类派生自Canvas,我在XAML中创建该类的实例。
<s:MyCanvas Focusable="true" Background="Transparent" x:Name="OwnCanvas" FocusVisualStyle="{x:Null}" ScrollViewer.CanContentScroll="True" >
I add command bindings this way
我以这种方式添加命令绑定
<UserControl.CommandBindings>
<CommandBinding Command="{x:Static ApplicationCommands.Copy}" CanExecute="event handler from object OwnCanvas" />
</UserControl.CommandBindings>
Is there any way to do that ? Or I have to transfer event handler directly to codebehind ??
有什么办法吗?还是必须将事件处理程序直接传输到codebehind ?
1 个解决方案
#1
0
I think you're gonna have to transfer the handler in codebehind as I don't think that's possible. I could be wrong and would love to be corrected if it is possible though.
我认为你必须在codebehind中转移处理器,因为我认为这是不可能的。我可能是错的,如果可能的话,我希望被纠正。
What I usually do is just define the CommandBinding in your MyCanvas class (code behind) and then reference that MyCanvas as the CommandTarget in the custom control. Like this:
我通常做的是在MyCanvas类(代码后面)中定义CommandBinding,然后在自定义控件中引用MyCanvas作为CommandTarget。是这样的:
public MyCanvas()
{
...
CommandBindings.Add(
new CommandBinding(ApplicationCommands.Copy,
(sender, e) => {
// Execute Stuff
},
(sender, e) => {
e.CanExecute = true;
e.Handled = true;
}));
...
}
And in your custom control (given it lies within the visual tree of MyCanvas)...
在您的自定义控件(给定它位于MyCanvas的可视树中)…
<Button Command="{x:Static ApplicationCommands.Copy}" CommandTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type s:MyCanvas}}}"/>
With your CommandTarget set up like that the Execute and CanExecute methods will be called on it.
使用您的CommandTarget设置,就会调用Execute和CanExecute方法。
#1
0
I think you're gonna have to transfer the handler in codebehind as I don't think that's possible. I could be wrong and would love to be corrected if it is possible though.
我认为你必须在codebehind中转移处理器,因为我认为这是不可能的。我可能是错的,如果可能的话,我希望被纠正。
What I usually do is just define the CommandBinding in your MyCanvas class (code behind) and then reference that MyCanvas as the CommandTarget in the custom control. Like this:
我通常做的是在MyCanvas类(代码后面)中定义CommandBinding,然后在自定义控件中引用MyCanvas作为CommandTarget。是这样的:
public MyCanvas()
{
...
CommandBindings.Add(
new CommandBinding(ApplicationCommands.Copy,
(sender, e) => {
// Execute Stuff
},
(sender, e) => {
e.CanExecute = true;
e.Handled = true;
}));
...
}
And in your custom control (given it lies within the visual tree of MyCanvas)...
在您的自定义控件(给定它位于MyCanvas的可视树中)…
<Button Command="{x:Static ApplicationCommands.Copy}" CommandTarget="{Binding RelativeSource={RelativeSource AncestorType={x:Type s:MyCanvas}}}"/>
With your CommandTarget set up like that the Execute and CanExecute methods will be called on it.
使用您的CommandTarget设置,就会调用Execute和CanExecute方法。