2012-03-12 76 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