2012-03-12 14 views
1

私はwpfとxaml(一般的なWindows開発)に新しく、私の背景はasp.netで、その古典的なaspの前です。処理が行われている間にボタンが無効にされているか灰色で表示されている必要があるアプリケーションで作業しています。ここでは次のように投稿を読みますが、動作していないようです。誰かが私が逃しているもので私を助けてくれますか?要求を処理中にボタンを無効にする

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

と:私は以下の持っているのViewModelで

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

<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を変更しましたありがとうございました_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

私は質問が既に回答されたと思いますが、私は 'が時にすることを指摘したいD \コマンドを使用すると、特定のコマンドに関連するコントロールを無効にするための 'CanExecute'メソッドを使うことができます。興味のある方は、[msdn](http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.canexecute.aspx)の情報を参照してください。 – icebat

答えて

2

、あなたはCommandプロパティにトリガーを設定しているが、あなたはボタンのそのプロパティにバインディングが設定されていない:

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

は次のようになります。

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

[ここでMyCommandはあなたがバインドしている実際のコマンドの名前です]

私はそれがとにかく動作するとは確信していませんが、 ertyはnullですが、コマンドプロパティにバインドするとMVVMパターンに従うと、コマンドプロパティがnullであってはならないので、トリガも起動しません。

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

+0

私はあなたからコードを得たと仮定しています:http://stackoverflow.com/questions/4138026/how-to-disable-button-when-its-button-commandproperty-is-null? その場合、CommandインターフェイスがCanExecute関数を使用しているため、その例が機能することに注意してください。したがって、コマンドが実行可能になると、コマンドはバインディングに戻され、それ以外の場合はnullになります。そういうわけで、それは彼らの例で働いているのです。これを実現する別の方法は、ViewModelにバッキングIsButtonEnabledプロパティを作成し、ボタンのIsEnabledプロパティをこのプロパティにバインドすることです。 – evasilchenko

+0

はい、私はどちらか、そのポストか、似たようなものでした。私は実際にビューモデルでプロパティを作成しました: private bool _isEnabled = true; public bool IsEnabled { get {return _isEnabled; } セット{_isEnabled =値; XAMLで} } : <ボタンのコンテンツ= "天気のインポートを実行" コマンド= "{バインディングExecuteWeather}" スタイル= "{StaticResourceのButtonStyle}" でIsEnabled = "{バインディングでIsEnabled}" /> 私はコードが実行されたときにプロパティをfalseに設定し、実行後もtrueに戻すがダイスは残さないようにします。 – Nathan

関連する問題