WPF加载指示器和两个按钮
嗨,我需要一件简单的事情 我需要两个按钮,开始,结束 按下开始加载指示灯时, 当按下End时,它应该停止 提前感谢您可以使用, 下面是一个非常简单的例子,说明你需要做什么(希望有帮助): 您的XAML-这是使用ViewModel中的ICommand绑定按钮的方式:WPF加载指示器和两个按钮,wpf,loading,wait,indicator,Wpf,Loading,Wait,Indicator,嗨,我需要一件简单的事情 我需要两个按钮,开始,结束 按下开始加载指示灯时, 当按下End时,它应该停止 提前感谢您可以使用, 下面是一个非常简单的例子,说明你需要做什么(希望有帮助): 您的XAML-这是使用ViewModel中的ICommand绑定按钮的方式: <StackPanel> <local:YourCustomBusyIndicator IsBusy="{Binding IsBusy}"/> <Button Content="Start
<StackPanel>
<local:YourCustomBusyIndicator IsBusy="{Binding IsBusy}"/>
<Button Content="Start" Command="{Binding StartCmd}"/>
<Button Content="End" Command="{Binding EndCmd}"/>
</StackPanel>
RoutedCommand更标准的方法是传递一个Func,该Func返回一个布尔值作为谓词,以便在CanExecute上调用显示您已经尝试过的内容。@user3134194 Stas的答案是否回答了您的问题?如果是这样,请接受它作为您问题的答案。
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;
}