开发者

wpf progressbar databind to command

I would like to use a progressbar 开发者_Python百科in a simple way. I have a query that is run to return data to a grid when a user clicks a button. I would like to start the progressbar when the button is clicked and stop the progressbar when the data is returned to the grid.

I just want the progressbar to continue on (IsIndeterminate="True") to show that there is actually something happening.

Is there any way to bind the start and stop of the progressbar to properties or commands in my view model?

Thanks for any thoughts.


Use the IsIndeterminate property as your property to bind against a property on your ViewModel; mine is titled IsBusy in this example.

    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
    }

The XAML is in this instance uses a click event handler for the Button; however in your instance you would simply bind your action which will start your processing to the command on your 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>

As you begin work and end work modifying the IsBusy property on your ViewModel will then start and stop the indeterminate behavior, providing the active/not-active visual appearance you are after.


You could expose a property that you then use to trigger the visibility of the ProgressBar, but you're better off using a control that encompasses a progress bar and exposes a property that turns it on/off. For example, the BusyIndicator in the Extended WPF Toolkit.

wpf progressbar databind to command

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜