![](/img/trans.png)
[英]WPF MVVM: How to disable clicked button and enable all other buttons?
[英]how to disable the other functions when Button clicked in wpf C#
I have a screen in WPF like that (WPF MVVM C#)I want to when I clicked the generate reports button.All of other functions will be disable.(without button)
我怎樣才能做到這一點? 我等待你的想法(就像在網格中)我可以像所有這些綁定 IsEnabled 一樣做到這一點。 但我不想那樣做。
<CheckBox x:Name="IncludeEmployeesCheckBox" Content="Include All Employees" IsChecked="{Binding IncludeAllEmployees, Mode=TwoWay}" Margin="4" />
<CheckBox Content="Exclude Weekends" IsChecked="{Binding ExcludeWeekends}" Margin="4" />
<CheckBox Content="Exclude Former Employees" IsChecked="{Binding ExcludeFormerEmployees}" Margin="4" />
<CheckBox Content="Exclude Public Holidays" IsChecked="{Binding ExcludePublicHolidays}" Margin="4" />
<Button Content="Generate Reports" Command="{Binding GenerateReportsCommand}" Height="36" Margin="4" />
感謝您的所有幫助。
假設您的報告生成 function 運行時間足夠長以證明使用 async / await 模式是合理的,請為 GenerateReportsCommand 使用異步命令 class。
public abstract class perCommandBase : ICommand
{
// ICommand members
public abstract bool CanExecute(object parameter);
public abstract void Execute(object parameter);
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
public class perRelayCommandAsync : perCommandBase, INotifyPropertyChanged
{
private readonly Func<Task> _execute;
private readonly Func<bool> _canExecute;
public perRelayCommandAsync(Func<Task> execute) : this(execute, () => true)
{
}
public perRelayCommandAsync(Func<Task> execute, Func<bool> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
private bool _isExecuting;
/// <summary>
/// Is the command currently executing
/// </summary>
public bool IsExecuting
{
get => _isExecuting;
set
{
_isExecuting = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsExecuting)));
RaiseCanExecuteChanged();
}
}
public override bool CanExecute(object parameter) => !IsExecuting && _canExecute();
public override async void Execute(object parameter)
{
if (!CanExecute(parameter))
{
return;
}
IsExecuting = true;
try
{
var response = await _execute()
.ExecuteActionWithTimeoutAsync(ExecuteTimeOut)
.ConfigureAwait(true);
if (response.IsTimedOut)
{
OnTimeOutAction?.Invoke(response);
}
else if (response.IsError)
{
OnErrorAction?.Invoke(response);
}
}
finally
{
IsExecuting = false;
}
}
/// <summary>
/// Timeout value for Execute invocation
/// </summary>
public TimeSpan ExecuteTimeOut { get; set; } = perTimeSpanHelper.Forever;
/// <summary>
/// Optional action to perform if Execute generates an error.
/// </summary>
public Action<perAsyncActionResponse> OnErrorAction { get; set; }
/// <summary>
/// Optional action to perform if Execute times out.
/// </summary>
public Action<perAsyncActionResponse> OnTimeOutAction { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
由於CanExecute
返回 false,當IsExecuting
為 true 時, Button
將自動禁用。 您還可以將其他控件的IsEnabled
或IsVisible
屬性綁定到GenerateReportsCommand.IsExecuting
。
關於如何將命令的 CanExecute 鏈接到我的博客文章中的其他屬性狀態的更多建議。
如果您使用 MVVM 模式,最好將 boolean 屬性與GenerateReportsCommand
等長時間運行的操作相關聯。 然后,您可以使用該布爾屬性來隱藏/禁用 XAML 中的控件。
例如:
private bool busy;
public bool Busy { get => this.busy; set { this.busy = value; OnPropertyChanged(); } }
private async Task GenerateReportsAsync()
{
// Set the Busy flag at the beginning of your long running operation
Busy = true;
// ... do your report generation here, which may take a long time
// Clear the Busy flag at the end of your long running operation
Busy = false;
}
在您的 xaml 中(我在這里使用 StackPanel,但任何 UIElement 都可以使用其 IsEnabled 或 Visibility 屬性:
<StackPanel>
<CheckBox/>
<CheckBox/>
<CheckBox/>
<StackPanel.Style>
<Style TargetType="StackPanel">
<Style.Triggers>
<DataTrigger Binding="{Binding Busy}" Value="True">
<Setter Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</Style.Triggers>
</Style>
</StackPanel.Style>
</StackPanel>
<Button Content="Generate Reports" Command="{Binding GenerateReportsCommand}" Height="36" Margin="4" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.