巴顿甚至在WPF提供CommandParameter后没有启用 [英] Button not enabled even after providing CommandParameter in WPF

查看:155
本文介绍了巴顿甚至在WPF提供CommandParameter后没有启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用实现ICommand接口的类的commandparameter设置和命令按钮。但我的按钮将被禁用。这是为什么?我得到这个code从这里开始:<一href="http://blogs.msdn.com/b/mikehillberg/archive/2009/03/20/icommand-is-like-a-chocolate-cake.aspx"相对=nofollow> ICommand的是像一个巧克力蛋糕

 &LT;窗​​口x:类=ICommand_Implementation_CSharp.MainWindow
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    XMLNS:地方=CLR的命名空间:ICommand_Implementation_CSharp
    标题=主窗口高度=350宽度=525&GT;
&LT;电网&GT;
    &LT;电网&GT;
        &LT; Grid.Resources&GT;
            &LT;地方:HelloWorldCommand X:关键=HWC/&GT;
        &LT; /Grid.Resources>

        &LT;按钮命令={的StaticResource HWC}CommandParameter =你好
                身高=23的Horizo​​ntalAlignment =左保证金=212,138,0,0
                NAME =Button1的VerticalAlignment =热门WIDTH =75&GT;按钮和LT; /按钮&GT;
    &LT; /网格&GT;
&LT; /网格&GT;
 

和我的课是

 类HelloWorldCommand:ICommand的
{
    公共BOOL CanExecute(对象参数)
    {
        返回参数!= NULL;
    }

    公共事件的EventHandler CanExecuteChanged;

    公共无效执行(对象参数)
    {
        的MessageBox.show(parameter.ToString());
    }
}
 

解决方案

那么,这是非常,非常简单的实现的ICommand 的。

由于@JleruOHeP说,部分问题是可以通过交换命令 CommandParameter 的制定者解决。但是,这是丑陋的方式,因为你每次都记住序列。

更正确的做法是告诉命令管理命令来重新查询状态:

 公共类HelloWorldCommand:ICommand的
{
    公共BOOL CanExecute(对象参数)
    {
        返回参数!= NULL;
    }

    公共事件的EventHandler CanExecuteChanged
    {
        添加{CommandManager.RequerySuggested + =价值; }
        除去{CommandManager.RequerySuggested  -  =价值; }
    }

    公共无效执行(对象参数)
    {
        的MessageBox.show(parameter.ToString());
    }
}
 

现在制定者的顺序是无动于衷。
要了解如何命令管理的作品,你可以阅读<一个href="http://joshsmithonwpf.word$p$pss.com/2008/06/17/allowing-commandmanager-to-query-your-icommand-objects/"相对=nofollow>这个的距离约什 - 史密斯不错的文章。

I created a button whose commandparameter is set and command using a class that implements ICommand interface. But my button is disabled. Why is that? I got this code from here: ICommand is like a chocolate cake

<Window x:Class="ICommand_Implementation_CSharp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:ICommand_Implementation_CSharp"
    Title="MainWindow" Height="350" Width="525">
<Grid>      
    <Grid>
        <Grid.Resources>
            <local:HelloWorldCommand x:Key="hwc"  />
        </Grid.Resources>

        <Button Command="{StaticResource hwc}" CommandParameter="Hello" 
                Height="23" HorizontalAlignment="Left" Margin="212,138,0,0" 
                Name="Button1" VerticalAlignment="Top" Width="75">Button</Button>
    </Grid>
</Grid>

and my class is

class HelloWorldCommand:ICommand
{
    public bool CanExecute(object parameter)
    {
        return parameter != null;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        MessageBox.Show(parameter.ToString());
    }
}

解决方案

Well, this is very-very simple implementation of ICommand.

As @JleruOHeP says, partially problem can be solved by swapping setters of Command and CommandParameter. But this is ugly way, because you have to remember the sequence every time.

More correct way is to tell CommandManager to re-query states of command:

public class HelloWorldCommand : ICommand
{
    public bool CanExecute(object parameter)
    {
        return parameter != null;
    }

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

    public void Execute(object parameter)
    {
        MessageBox.Show(parameter.ToString());
    }
}

Now the sequence of setters is indifferent.
To understand, how CommandManager works, you can read this nice article from Josh Smith.

这篇关于巴顿甚至在WPF提供CommandParameter后没有启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆