WPF加载指示器和两个按钮



在此处输入图像描述嗨,我需要一个简单的事情我需要两个按钮,开始,结束当按下启动加载指示器时,按下结束时应该停止预先感谢

您可以使用icommand-pattern,以下是您需要做的事情的一个非常幼稚的例子(希望它有帮助):

您的XAML-这是您使用ViewModel的ICommand绑定按钮的方式:

<StackPanel>
    <local:YourCustomBusyIndicator IsBusy="{Binding IsBusy}"/>
    <Button Content="Start" Command="{Binding StartCmd}"/>
    <Button Content="End" Command="{Binding EndCmd}"/>
</StackPanel>

您查看模型代码:

public class YourViewModel : INotifyPropertyChanged
{
    private bool _isBusy;
    public bool IsBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;
            OnPropertyChanged();
        }
    }
    public RoutedCommand StartCmd { get; }
    public RoutedCommand EndCmd { get; }
    public YourViewModel()
    {
        StartCmd = new RoutedCommand(() => IsBusy = true);
        EndCmd = new RoutedCommand(() => IsBusy = false);
    }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

//Simple implementation of ICommand
public class RoutedCommand :ICommand
{
    private readonly Action _onExecute;
    public RoutedCommand(Action onExecute)
    {
        _onExecute = onExecute;
    }
    public bool CanExecute(object parameter)
    {
        return true;
    }
    public void Execute(object parameter)
    {
        _onExecute();
    }
    public event EventHandler CanExecuteChanged;
}

此外,RoutedCommand的更为标准方法也将通过一个弹性,该功能返回布尔值作为谓词,以调用canexecute

相关内容

  • 没有找到相关文章

最新更新