Wpf 在处理请求时禁用按钮
我不熟悉wpf和xaml(一般的Windows开发),我的背景是asp.net,并且在那个经典的asp.net之前 我正在处理一个应用程序,需要在处理过程中禁用/灰显按钮,并在此处阅读一篇文章以执行以下操作,但它似乎不起作用。我错过了什么Wpf 在处理请求时禁用按钮,wpf,xaml,Wpf,Xaml,我不熟悉wpf和xaml(一般的Windows开发),我的背景是asp.net,并且在那个经典的asp.net之前 我正在处理一个应用程序,需要在处理过程中禁用/灰显按钮,并在此处阅读一篇文章以执行以下操作,但它似乎不起作用。我错过了什么 <Window x:Class="SCGen.Application.LoadForecast.EngineExecution" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen
<Window x:Class="SCGen.Application.LoadForecast.EngineExecution"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:igEditors="http://infragistics.com/Editors"
SizeToContent="WidthAndHeight"
Title="Engine Execution"
ResizeMode="NoResize"
WindowStartupLocation="CenterOwner"
Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}">
<Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="myStyle" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="Command" Value="{Binding ExecuteEngine}" />
<Setter Property="Content" Value="Execute Engine" />
<Style.Triggers>
<Trigger Property="Command" Value="{x:Null}">
<Setter Property="IsEnabled" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Border Padding="8">
<StackPanel>
<StackPanel MaxWidth="200" HorizontalAlignment="Left">
<TextBlock Text="Select Forecast Engine" TextAlignment="Center" FontSize="13" />
<igEditors:XamComboEditor ItemsSource="{Binding ForecastEngines}" SelectedItem="{Binding SelectedEngine}" Margin="0,5" />
<Button Style="{StaticResource ResourceKey=myStyle}" />
</StackPanel>
<TextBlock Text="{Binding EngineStatus}" FontSize="15" FontStyle="Italic" Margin="0,14" Width="400" TextWrapping="Wrap" />
</StackPanel>
</Border>
</Window>
我在这里设置了可访问的:
private string LaunchWeatherImport(string strVendor)
{
_isEnabled = false;
string uri = ConfigurationManager.AppSettings["ManualExecutionFacilitatorService"];
ClientConnectionInfo connection = new ClientConnectionInfo(uri) { UseSecurity = true };
connection.SetTimeouts();
Logger.LogInfo("Calling Facilitator service to manually import " + strVendor + " weather data.");
((NetTcpBinding)connection.Binding).Security.Mode = System.ServiceModel.SecurityMode.None;
using (var client = new FacilitatorManualExecutionClient(connection))
{
client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(int.Parse(ConfigurationManager.AppSettings["OperationTimeOutMinutes"]));
try
{
_isEnabled = true;
return "success";
// uncomment this line before commit
//return client.ExecuteWeather(strVendor);
}
#region catch
catch (Exception ex)
{
Logger.LogError(ex.Message, ex);
return ex.Message;
}
#endregion
}
}
我还是不能让它正常工作 对于初学者,您正在命令属性上设置触发器,但没有为按钮在该属性上设置绑定:
<Button Style="{StaticResource ResourceKey=myStyle}" />
然后添加实现:
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
然后将您的属性更改为:
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
NotifyPropertyChanged("IsEnabled");
}
}
你好,谢谢你的回复。我把按钮标签改成了你上面提到的方式,但仍然没有骰子。此应用程序确实遵循MVVM,在此之前,我没有使用过MVVM、WPF或Xaml。为了使触发器正常工作,触发器应该使用什么而不是x:Null?我假设您的代码来自:?如果是这种情况,那么您应该注意,他们的示例是有效的,因为他们的命令接口使用CanExecute函数。因此,当命令可以执行时,命令将返回到绑定,否则为null。这就是为什么在他们的例子中它是有效的。另一种方法是在ViewModel中创建一个IsButtonEnabled属性,并将按钮的IsEnabled属性绑定到此属性。是的,我做了,要么是那篇文章,要么是另一篇类似的文章。我实际上在viewmodel中创建了一个属性:private bool _isEnabled=true;public bool IsEnabled{get{return}IsEnabled;}set{{IsEnabled=value;}在xaml中:当代码执行时,我将属性设置为false,执行后返回true,但仍然没有骰子。您需要在具有该属性的类中实现INotifyPropertyChanged接口,然后在设置后调用属性中的notify函数。这将让您的UI知道属性已更改,因此需要重新绘制UI并进行更新。感谢您的帮助。我已经按照你的建议做了,现在应该可以用了。此ViewModel继承自一个基类,该基类内部实现了INotifyPropertyChanged接口。我添加了对RaisePropertyChanged(“IsButtonneEnabled”)的调用;在我的属性设置器中。我感谢你的帮助。看起来按钮没有被禁用,但我认为这是因为代码执行速度太快,值在毫秒内切换。我想问题已经得到了回答,但我想指出的是,当使用命令时,您可以使用
CanExecute
方法来禁用与特定命令相关的控件。如果有兴趣,你可以阅读一些有关的信息。
<Button Style="{StaticResource ResourceKey=myStyle}" Command="{Binding MyCommand}" />
public class MyClass : System.ComponentModel.INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private bool _isEnabled = true;
public bool IsEnabled
{
get { return _isEnabled; }
set
{
_isEnabled = value;
NotifyPropertyChanged("IsEnabled");
}
}