wpf进度条数据绑定到命令

发布于 2024-11-24 06:36:23 字数 197 浏览 0 评论 0原文

我想以简单的方式使用进度条。我有一个查询,当用户单击按钮时,该查询将运行以将数据返回到网格。我想在单击按钮时启动进度条,并在数据返回到网格时停止进度条。

我只是希望进度条继续(IsInminateate =“True”)以表明确实发生了一些事情。

有没有办法将进度条的开始和停止绑定到我的视图模型中的属性或命令?

感谢您的任何想法。

I would like to use a progressbar 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2024-12-01 06:36:23

使用 IsInminateate 属性作为您的属性来绑定 ViewModel 上的属性;在此示例中,我的标题为 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 属性将开始停止不确定行为,提供活动/非-您所追求的活跃视觉外观。

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.

别低头,皇冠会掉 2024-12-01 06:36:23

您可以公开一个属性,然后使用该属性来触发 ProgressBar 的可见性,但最好使用包含进度条的控件并公开用于打开/关闭进度条的属性。例如,BusyIndi​​cator 中扩展的 WPF 工具包。

在此处输入图像描述

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.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文