2012-03-12 9 views
1

wpf 및 xaml (일반적으로 Windows 개발)에 익숙하지 않으며 내 배경은 asp.net이고 이전 asp는 이전입니다. 처리가 발생하는 동안 비활성화 된/회색으로 표시된 단추가 있어야하는 응용 프로그램에서 작업 중이며 여기에서 게시물을 읽으면 다음 작업을 수행 할 수 있지만 작동하지 않는 것으로 보입니다. 누군가 내가 놓친 걸 도와 주시겠습니까?요청 처리 중 버튼 사용 안 함

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

과 : 나는 다음이 뷰 모델에서

<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

나는 질문은 이미 대답 추측,하지만 난 '때 점을 지적하고 싶습니다 \ 커맨드를 사용하면 특정 커맨드와 관련된 컨트롤을 비활성화하기 위해'CanExecute' 메소드를 사용할 수 있습니다. 관심이 있다면 [msdn] (http://msdn.microsoft.com/en-us/library/system.windows.input.routedcommand.canexecute.aspx)에서 몇 가지 정보를 읽을 수 있습니다. – icebat

답변

2

, 당신이 명령 속성에 트리거를 설정하는 그러나 당신이 당신의 버튼에 해당 속성에 바인딩이 설정되지 않습니다

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

은 다음과 같아야합니다

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

[여기서 MyCommand는 바인딩중인 실제 명령의 이름입니다]

나는 어쨌든 작동 할 것이라고 확신하지는 않습니다. 왜냐하면 트리거가 설정되기 때문에 트리거가 설정되어 있기 때문에 명령 prop erty가 null이지만 명령 속성에 바인딩하면 MVVM 패턴 다음에 명령 속성이 null이 아니어야 트리거가 실행되지 않습니다.

업데이트 :

당신은 속성이 클래스에 INotifyPropertyChanged 인터페이스를 구현해야합니다. 로 그런 다음 속성을 변경

public event PropertyChangedEventHandler PropertyChanged; 

private void NotifyPropertyChanged(String info) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

:

public class MyClass : System.ComponentModel.INotifyPropertyChanged 

그런 다음 구현을 추가

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? 그렇다면 명령 인터페이스가 CanExecute 기능을 사용하기 때문에 예제가 작동한다는 것을 알아 두어야합니다.그래서 명령이 실행될 수 있으면 명령은 바인딩으로 리턴되고, 그렇지 않으면 널입니다. 그것이 그들의 예에서 효과가있는 이유입니다. 이 작업을 수행하는 또 다른 방법은 ViewModel에 Backing IsButtonEnabled 속성을 만들고 단추의 IsEnabled 속성을이 속성에 바인딩하는 것입니다. – evasilchenko

+0

예, 저 게시물이나 비슷한 게시물 중 하나를 사용했습니다. 실제로 뷰 모델에서 속성을 만들었습니다. private bool _isEnabled = true; 공개 bool IsEnabled { get {return _isEnabled; } 세트 {_isEnabled = 값; } }는 XAML에서 : 난 <버튼 내용 = "날씨 가져 오기 실행"명령 = "{바인딩 ExecuteWeather}"스타일 = "{정적 리소스 ButtonStyle은}"의 IsEnabled은 =/"{}의 IsEnabled 바인딩"> 코드가 실행될 때 속성을 false로 설정하고 실행 후에도 true로 설정하지만 주사위는 여전히 사용하지 않습니다. – Nathan