2011-07-14 23 views
0

私は簡単な方法でプログレスバーを使いたいと思います。ユーザーがボタンをクリックしたときにデータをグリッドに返すクエリが実行されています。ボタンをクリックするとプログレスバーを開始し、データがグリッドに戻ったときにプログレスバーを停止したいと思います。wpf progressbarコマンドにデータバインド

実際に何か起こっていることを示すために、進捗バーに(IsIndeterminate = "True")を続けるだけです。

ビューモデルのプロパティまたはコマンドにプログレスバーの開始と停止をバインドする方法はありますか?

ありがとうございました。

答えて

2

ProgressBarの可視性をトリガーするプロパティを公開することはできますが、プログレスバーを含むコントロールを使用してオン/オフを切り替えるプロパティを公開する方がよい場合があります。たとえば、拡張WPFツールキットのBusyIndicatorです。

enter image description here

+0

私はビジー指標をチェックアウトします。ありがとう – czuroski

2

あなたのViewModelのプロパティに対してバインドするためにあなたの財産としてIsIndeterminateプロパティを使用します。この例では私の名前はIsBusyです。

public partial class Window1 : Window 
    { 
     public MyViewModel _viewModel = new MyViewModel(); 

     public Window1() 
     { 
      InitializeComponent(); 

      this.DataContext = _viewModel; 
     } 

     private void Button_Click(object sender, RoutedEventArgs e) 
     { 
      //this would be a command in your ViewModel, making life easy 
      _viewModel.IsBusy = !_viewModel.IsBusy; 
     } 
    } 

    public class MyViewModel : INotifyPropertyChanged 
    { 
     private bool _isBusy = false; 
     public bool IsBusy 
     { 
      get 
      { 
       return _isBusy; 
      } 
      set 
      { 
       _isBusy = value; 
       PropertyChangedEventHandler handler = PropertyChanged; 
       if(handler != null) 
        handler(this, new PropertyChangedEventArgs("IsBusy")); 
      } 
     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 

     #endregion 
    } 

XAMLはButtonのクリックイベントハンドラを使用して、このインスタンスです。あなたのインスタンスでは、アクションをバインドするだけで、ViewModelのコマンドに処理が開始されます。

<Grid> 
      <ProgressBar Width="100" Height="25" IsIndeterminate="{Binding IsBusy}"></ProgressBar> 
      <Button VerticalAlignment="Bottom" Click="Button_Click" Width="100" Height="25" Content="On/Off"/> 
    </Grid> 

あなたのViewModelにIsBusyプロパティを変更する作業と終了作業が続いあなたが後にしているアクティブ/アクティブでない外観を提供し、停止に不確定な行動を開始します始めると。

関連する問題