非根对象中的XAML事件处理程序

非根对象中的XAML事件处理程序,xaml,uwp-xaml,Xaml,Uwp Xaml,想象一下下面的XAML: <Page x:Class="MyProject.MainPage" xmlns:local="using:MyProject"> <local:MyStackPanel> <Button Content="Go" Click="OnGo"/> </local:MyStackPanel> </Page> XAML假定OnGo是page类中的一个方法。我是否可以用

想象一下下面的XAML:

<Page
    x:Class="MyProject.MainPage"
    xmlns:local="using:MyProject">

   <local:MyStackPanel>
       <Button Content="Go" Click="OnGo"/>
   </local:MyStackPanel>
</Page>

XAML假定
OnGo
是page类中的一个方法。我是否可以用声明的方式告诉它,OnGo驻留在
MyStackPanel


我知道我可以在加载页面后以编程方式分配事件处理程序。我想知道是否可以通过纯粹的XAML方法实现。“否”是一个可接受的答案:)

根据您的要求,您可以将MyStackPanel
DataContext
本身绑定起来。然后创建用于在
MyStackPanel
类中绑定按钮命令的
BtnClickCommand
,如下所示

public class MyStackPanel : StackPanel
{
    public MyStackPanel()
    {

    }

    public ICommand BtnClickCommand
    {
        get
        {
            return new RelayCommand(() =>
            {


            });
        }
    }

}
public class RelayCommand : ICommand
{
    private readonly Action _execute;
    private readonly Func<bool> _canExecute;
    /// <summary>
    /// Raised when RaiseCanExecuteChanged is called.
    /// </summary>
    public event EventHandler CanExecuteChanged;
    /// <summary>
    /// Creates a new command that can always execute.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    public RelayCommand(Action execute)
        : this(execute, null)
    {
    }
    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action execute, Func<bool> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }
    /// <summary>
    /// Determines whether this RelayCommand 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>
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute();
    }
    /// <summary>
    /// Executes the RelayCommand on the current command target.
    /// </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>
    public void Execute(object parameter)
    {
        _execute();
    }
    /// <summary>
    /// Method used to raise the CanExecuteChanged event
    /// to indicate that the return value of the CanExecute
    /// method has changed.
    /// </summary>
    public void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
}
公共类MyStackPanel:StackPanel
{
公共事务委员会()
{
}
公共ICommand BtnClickCommand
{
得到
{
返回新的RelayCommand(()=>
{
});
}
}
}
公共类中继命令:ICommand
{
私有只读操作\u执行;
私有只读功能可执行;
/// 
///调用RaiseCanExecuteChanged时引发。
/// 
公共事件处理程序CanExecuteChanged;
/// 
///创建始终可以执行的新命令。
/// 
///执行逻辑。
公共中继命令(操作执行)
:此(执行,空)
{
}
/// 
///创建一个新命令。
/// 
///执行逻辑。
///执行状态逻辑。
公共RelayCommand(操作执行,函数执行)
{
if(execute==null)
抛出新的ArgumentNullException(“执行”);
_执行=执行;
_canExecute=canExecute;
}
/// 
///确定此RelayCommand是否可以在其当前状态下执行。
/// 
/// 
///命令使用的数据。如果命令不要求传递数据,
///此对象可以设置为null。
/// 
///如果可以执行此命令,则为true;否则为false。
公共布尔CanExecute(对象参数)
{
return _canExecute==null?true:_canExecute();
}
/// 
///在当前命令目标上执行RelayCommand。
/// 
/// 
///命令使用的数据。如果命令不要求传递数据,
///此对象可以设置为null。
/// 
public void Execute(对象参数)
{
_执行();
}
/// 
///用于引发CanExecuteChanged事件的方法
///指示CanExecute的返回值
///方法改变了。
/// 
public void raisecancecutechanged()
{
var handler=CanExecuteChanged;
if(处理程序!=null)
{
处理程序(此,EventArgs.Empty);
}
}
}
用法

<local:MyStackPanel DataContext="{Binding RelativeSource={RelativeSource Mode=Self}}">
    <Button Command="{Binding BtnClickCommand}" Content="ClickMe" />
</local:MyStackPanel>


谢谢。但几乎不是声明性的。@SevaAlekseyev这个答案不符合要求吗?没有要求,只是希望XAML比我知道的更灵活:)显然不是。@SevaAlekseyev,好吧,XAML是不灵活的。它不能做你想做的事。欢迎你在网上发帖。