WPF加载指示灯和两个按钮

时间:2016-12-12 10:45:30

标签: wpf loading wait indicator

enter image description here 嗨,我需要一件简单的事情 我需要两个按钮,Start,End 当按下开始加载指示出现时, 当按下End时它应该停止 提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用ICommand-pattern, 以下是您需要做的一个非常天真的例子(希望它有所帮助):

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

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

您的ViewModel代码:

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的更标准方法也是传递一个Func,它返回一个布尔值作为谓词在CanExecute上调用