2010-03-29 97 views
0

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

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类中。

而且我希望当按钮,所以我输入结合产生点击鼠标触发事件,该命令是正确的吗?

感谢

回答

1

既然你有一个ICommand,你可以将它绑定到通过命令属性按钮,该按钮将使用它,也就是说,它会调用CanExecute启用/禁用本身并在Execute方法按钮。不需要任何其他输入绑定。

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

如果DataContext的有一个叫类型RemoveAllCommand的removeall过的财产,你可以简单地改变你的XAML按钮:

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

和删除的CommandBinding和InputBinding

+0

嘿,我已经做到了,但没有成功,我在构建项目时收到错误:无法创建类型为RemoveAllCommand的实例。 我在发布的答案中解释它。 – toni 2010-03-29 11:43:13

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() { } 

    ... 
} 

感谢。

+1

这里的问题是你应该使用{StaticResource RemoveAllCommandInstance}来获取命令。 – Timores 2010-03-29 16:12:54

0

oK,谢谢,明天我会试试看。

我们避免我已经全押RemoveAllCommandClass类来我的页面背后的代码的问题,我也做了一些修改。

1 .-我说这MyPage.xaml:

的xmlns:地方= “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}" .../> 

,一切都OK和工作。当我按下按钮时,它执行在Execute方法中调用的后台工作者(bw)。 bw进入另一个班级。在后台工作者类中,我有一个变量(isRunning),指示bw是否正在执行。在执行DoWork事件之前,我将它设置为true,当bw完成时,在RunWorkerCompleted上,我将它设置为false。所以从CanExecute我检查isRunning在bw类,我设置为true e.canExecute如果isRunning为false,并且如果isRunning为true,则e.canExecute为false。所以当bw运行时按钮被WPF自动禁用,但是当bw完成时按钮继续被禁用,并且不会返回到启用状态,直到我再次按下按钮。为什么WPF没有将按钮状态更新为在bw结束时启用,直到我再次按下按钮?

谢谢。

+0

您应该告诉WPF命令状态已更改。有这个CanExecuteChanged事件。 – Timores 2010-03-31 12:52:25