0

我已经为我的一个按钮实现了一个自定义 IComand 类。该按钮被放置在页面“MyPage.xaml”中,但其自定义 ICommand 类被放置在另一个类中,而不是在 MyPage 代码后面。然后从 XAML 我想将按钮与它的自定义命令类绑定,然后我这样做:

MyPage.xaml:

    <Page ...>

         <Page.CommandBindings>
             <CommandBinding Command="RemoveAllCommand"
                CanExecute="CanExecute"
                Executed="Execute" />
         </Page.CommandBindings>

         <Page.InputBindings>
                <MouseBinding Command="RemoveAllCommand" MouseAction="LeftClick" />
         </Page.InputBindings>

             <...>
               <Button x:Name="MyButton"  Command="RemoveAllCommand" .../>
             <...>

    </Page>

和自定义命令按钮类:

// Here I derive from MyPage class because I want to access some objects from 
// Execute method
public class RemoveAllCommand : MyPage, ICommand 
{

    public void Execute(Object parameter)
    {
        <...>
    }

    public bool CanExecute(Object parameter)
    {
        <...>
    }


    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }  

}

我的问题是如何说 MyPage.xaml 按钮的 Execute 和 CanExecute 方法在另一个类中,而不是按钮所在位置的代码。怎么说这些方法在 XAML 页面的 RemoveAllCommand 类中。

我还想在按钮中产生单击鼠标事件时触发此命令,所以我进行输入绑定,是否正确?

谢谢

4

3 回答 3

1

既然你有一个ICommand,你可以通过Command 属性将它绑定到Button,并且按钮会使用它,即它会调用CanExecute 来启用/禁用它自己以及当按钮被按下时的Execute 方法。不需要任何其他输入绑定。

现在的问题是按钮必须找到命令。一个简单的解决方案是将命令的实例放在按钮(或其父项)的 DataContext 中。

如果 DataContext 具有类型为 RemoveAllCommand 的名为 RemoveAll 的属性,则可以简单地将 XAML 按钮更改为:

<Button Command="{Binding RemoveAll}" .. />

并删除 CommandBinding 和 InputBinding

于 2010-03-29T11:02:18.587 回答
0

嘿,我已经完成了,但没有成功,我在构建项目时收到错误:无法创建 RemoveAllCommand 类型的实例。

我已经做了:

MyPage.xaml(我已从页面中删除 CommandBinding 和 InputBinding):

<Page  x:Class="GParts.Pages.MyPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;
assembly=PresentationFramework.Aero"            
xmlns:Classes="clr-namespace:GParts.Classes"   
xmlns:Pages="clr-namespace:GParts.Pages">

<Page.Resources>
      <...>
         <Pages:RemoveAllCommand x:Key="RemoveAllCommandInstance"/>
      <...>
</Page.Resources>

<...>
<Button Command="{Binding RemoveAllCommandInstance}" ...>  
<...>
</Page>

在自定义 Icommand 类中,我添加了一个不带参数的构造函数:

public class RemoveAllCommand : MyPage, ICommand 
{

    public RemoveAllCommand() { }

    ...
}

谢谢。

于 2010-03-29T11:42:01.900 回答
0

好的,谢谢,明天我试试。

现在为避免出现问题,我已将 RemoveAllCommandClass 类中的所有内容移至 MyPage 后面的代码中,并进行了一些修改。

1.- 我将此添加到 MyPage.xaml:

xmlns:local="clr-命名空间:GParts"

然后我做了:

<Page.CommandBindings>
    <CommandBinding Command="{x:Static local:Pages.MyPage._routedCommand}"
               Executed="Execute"
               CanExecute="CanExecute"/>
</Page.CommandBindings>


<Button Command="{x:Static local:Pages.MyPage._routedCommand}"  .../>

一切都很好并且工作正常。当我按下按钮时,它会执行在 Execute 方法中调用的后台工作程序(bw)。bw 进入另一个班级。在后台工作人员类中,我有一个变量(isRunning),指示 bw 是否正在执行。在执行 DoWork 事件之前,我将其设置为 true,当 bw 完成时,在 RunWorkerCompleted 处,我将其设置为 false。因此,我从 CanExecute 检查 bw 类中的 isRunning,如果 isRunning 为假,我将 e.canExecute 设置为真,如果 isRunning 为真,则将 e.canExecute 设置为假。因此,当 bw 运行时,WPF 会自动禁用该按钮,但是当 bw 完成时,该按钮将继续禁用,并且在我再次按下它之前不会恢复为启用状态。为什么在我再次按下按钮之前,当 bw 完成时,WPF 不会将按钮状态更新为启用?

谢谢。

于 2010-03-29T19:40:02.117 回答