2012-03-12 74 views
1

我是wpf和xaml(一般Windows开发)的新手,我的背景是asp.net,并且在那个经典asp之前。我正在研究一个应用程序,当处理发生时需要将按钮禁用/变为灰色,并在此处阅读帖子以执行以下操作,但它看起来不起作用。请有人帮助我,我失踪了吗?处理请求时禁用按钮

<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> 

谢谢

我已经改变了XAML为以下:

<Button Content="Execute Weather Import" Command="{Binding ExecuteWeather}" Style="{StaticResource ButtonStyle}" IsEnabled="{Binding IsEnabled}"/> 

在视图模型我有以下几点:

private bool _isEnabled = true; 
    public bool IsEnabled 
    { 
     get { return _isEnabled; } 
     set { _isEnabled = value; } 
    } 

和我设置_isEnabl在这里编辑:

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 
     } 
    } 

我仍然无法正常工作。对不起,不得不添加到此,但评论的回复字段不足以发布代码。

+0

我想问题已经回答了,但我\'倒要指出的是,当使用命令您可以使用'CanExecute'方法来禁用与特定命令关联的控件。如果感兴趣,可以阅读[msdn](http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.canexecute.aspx)上的一些信息。 – icebat 2012-03-12 15:18:59

回答

2

对于初学者来说,你设置的Command属性触发,但你没有对财产的按钮绑定集:

<Button Style="{StaticResource ResourceKey=myStyle}" /> 

应该是:

<Button Style="{StaticResource ResourceKey=myStyle}" Command="{Binding MyCommand}" /> 

[其中MyCommand是你绑定的实际命令的名称]

我不太确定它会无论如何都能正常工作,不过因为你的触发器被设置为在命令道具erty为null,但如果绑定到命令属性,则遵循MVVM模式,那么您的命令属性不应该为空,因此触发器也不会触发。

UPDATE:

你需要实现你的类,它具有属性的INotifyPropertyChanged接口。

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"); 
    } 
} 
+0

嗨,谢谢你的回复。我改变了按钮标签的方式,你上面提到的方式,仍然没有骰子。这个应用程序确实遵循MVVM,并且在此之前我没有使用过MVVM,WPF或Xaml。触发器应该使用什么而不是x:Null使其正常工作? – Nathan 2012-03-12 14:17:16

+0

我假设你从下面的代码:http://stackoverflow.com/questions/4138026/how-to-disable-button-when-its-button-commandproperty-is-null? 如果是这种情况,那么你应该注意他们的例子工作,因为他们的Command接口正在使用CanExecute函数。所以当命令可以执行时,该命令返回到绑定,否则它是空的。这就是为什么它在他们的例子中起作用。另一种可以完成此操作的方法是在ViewModel中创建一个支持IsButtonEnabled属性,并将您的按钮的IsEnabled属性绑定到此属性。 – evasilchenko 2012-03-12 14:31:25

+0

是的,我做过了,无论是该帖子还是另一个相似的帖子。我实际上在viewmodel中创建了一个属性: private bool _isEnabled = true; public bool IsEnabled { get {return _isEnabled; } set {_isEnabled = value; }} 在XAML: <按钮内容= “执行天气导入” 命令= “{结合ExecuteWeather}” 样式= “{的StaticResource的ButtonStyle}” 的IsEnabled = “{结合的IsEnabled}”/> 我在代码执行时将该属性设置为false,并在执行后将该属性恢复为true,但仍然没有骰子。 – Nathan 2012-03-12 14:33:43