WPF 加载指示器和两个按钮
WPF loading indicator and two buttons
enter image description here
嗨,我需要一个简单的东西
我需要两个按钮,开始,结束
当按下开始加载指示器出现时,
按结束时它应该停止
提前致谢
你可以使用 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,其中 returns 一个布尔值作为在 CanExecute 上调用的谓词
enter image description here 嗨,我需要一个简单的东西 我需要两个按钮,开始,结束 当按下开始加载指示器出现时, 按结束时它应该停止 提前致谢
你可以使用 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,其中 returns 一个布尔值作为在 CanExecute 上调用的谓词