【问题标题】:Button is enabled only when the other button was clicked wpf datagrid仅当单击另一个按钮时才启用按钮 wpf datagrid
【发布时间】:2018-05-23 09:19:22
【问题描述】:

我有 2 个按钮:

在我看来 MainView.xaml

<StackPanel Grid.Row="2" Grid.Column="3" VerticalAlignment="Center">
                <Button Name="wec" Height="50" Content="Podgląd" Margin="15 15 15 0" Command="{Binding ViewCommand}"/>
                <Button Height="50" Content="Drukuj" Margin="15 15 15 0"  Command="{Binding ElementName=pv, Path=PrintCommand}">
                </Button>
            </StackPanel>
            <local:PrintPreview Grid.Row="4" x:Name="pv" Grid.ColumnSpan="3" PrintingClass="{Binding Model.PrintingClass, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" PrintModel="{Binding Model.PrintingModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

我的第一个按钮 - 命令查看文档的打印方式。 第二个按钮 - 命令从 PrintPreview.xaml.cs 打印此文档和下面的代码

private DelegateCommand printCommand;
    public DelegateCommand PrintCommand
    {
        get
        {

            if (printCommand == null)
                printCommand = new DelegateCommand(print);
            return printCommand;
        }
        set
        {
            if (printCommand != value)
            {
                printCommand = value;
            }
        }
    }

    public bool IsFirstButtonClicked { get; set; }

    private bool PrintCommandCanExecute(object unused)
    {
        return this.IsFirstButtonClicked;
    }

 private void print(object x)
    {
        setPageSettings();
        WB.Print();
    }

这并不完全,但我希望这足以解决解决方案:)

我试过但我不能这样做,意思是:我希望第二个按钮只有在第一个按钮被点击时才处于活动状态。您有什么简单的想法可以做到这一点吗?

【问题讨论】:

    标签: wpf button datagrid isenabled


    【解决方案1】:

    首先,创建一个 bool 属性,以便您知道何时单击了第一个按钮,我们称之为 IsFirstButtonClicked。

    在您的 Print 命令中,您应该添加一个 CommandCanExecute ,例如:

    private bool SecondCommandCanExecute(object unused)
     {
       return this.IsFirstButtonClicked;
     }
    

    当您单击第一个按钮时,您将 IsFirstButtonClicked(布尔属性)设置为 true 并提高您的 PrintCommandCanExecute。


    我猜你有一个 ICommand FirstCommand。你应该像这样创建一个类:

    public class RelayCommand : ICommand
    {
    private Action<object> _action;
    private Func<bool> _func;  
    
    public RelayCommand(Action<object> action,Func<bool> func)
    {
        _action = action;
        _func = func;
    }
    
    public void RaiseCanExecuteChanged()
    {
        if(CanExecuteChanged!=null)
            CanExecuteChanged(this,new EventArgs());
    }
    
    #region ICommand Members
    
    public bool CanExecute(object parameter)
    {
        if (_func != null)
           return _func();
        return true;
    }
    
    
    
    public event EventHandler CanExecuteChanged;
    
    public void Execute(object parameter)
    {
        _action(parameter);
    }
    
    #endregion
    }
    

    现在您的 SecondCommand 应该是这样的 RelayCommand:

    RelayCommand SecondCommand = new RelayCommand(yourCommandExecute, this.SecondCommandCanExecute)
    

    现在,当您单击 FirstButton 时,将 IsFirstButtonClicked 设置为 true,然后调用 SecondCommand.RaiseCanExecuteChanged();

    如果对你有帮助,请告诉我。

    【讨论】:

    • 不知道理解够不够:我加了一个bool属性,接下来我加comman可以执行。那么按钮呢?我想我必须以某种方式执行这个命令才能运行它?
    • 用你的命令代码编辑你的操作,我会更新我的答案来澄清它:)
    • 在我编辑我的 OP 的同时 :) 但非常感谢你
    • 更改此公共 bool IsFirstButtonClicked {get;设置;} 到这个 -> 私有 bool IsFirstButtonClicked;当您单击第一个按钮时,将 IsFirstButtonClicked 设置为 true,然后执行此 SecondCommand.RaiseCanExecuteChanged()(现在我认为您将其称为 PrintView)
    猜你喜欢
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多