c# - 使用 MVVM 的 BusyIndi​​cator

标签 c# .net wpf mvvm wpftoolkit

这可能是重复的问题,但我找不到解决问题的方法。

我正在使用 MVVM 模式开发 WPF 应用程序。

有四个 View 绑定(bind)到它们的 ViewModel。所有 ViewModel 都将 BaseViewModel 作为父级。

public abstract class ViewModelBase : INotifyPropertyChanged
{
    private bool isbusy;

    public bool IsBusy
    {
        get
        {
            return isbusy;
        }
        set
        {
            isbusy = value;
            RaisePropertyChanged("IsBusy");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

MainView 包含 BusyIndi​​cator:

<extWpfTk:BusyIndicator IsBusy="{Binding IsBusy}">
        <ContentControl />
    </extWpfTk:BusyIndicator>

如果我在 MainViewModel 中设置 IsBusy = true,则显示 BusyIndi​​cator。

如果我尝试从其他 ViewModel 设置 IsBusy = true,则不会显示 BusyIndi​​cator。

请注意,我不能在我的项目中使用 MVVMLight 等第 3 方库,以便使用他们的 Messenger 在 ViewModel 之间进行通信。

主视图:

public class MainWindowViewModel : ViewModelBase
{
    public ViewModel1 ViewModel1 { get; set; }
    public ViewModel2 ViewModel2 { get; set; }
    public ViewModel3 Model3 { get; set; }

    public MainWindowViewModel()
    {
        ViewModel1 = new ViewModel1();
        ViewModel2 = new ViewModel2();
        ViewModel3 = new ViewModel3();
        //IsBusy = true; - its working
    }
}

ViewModel1:

public class ViewModel1 : ViewModelBase
{
    RelayCommand _testCommand;

    public ViewModel1()
    {
    }

    public ICommand TestCommand
    {
        get
        {
            if (_testCommand == null)
            {
                _testCommand = new RelayCommand(
                    param => this.Test(),
                    param => this.CanTest
                    );
            }
            return _testCommand;
        }
    }

    public void Test()
    {
        //IsBusy = true; - BusyIndicator is not shown

    }

    bool CanTest
    {
        get 
        { 
            return true; 
        }
    }
}

最佳答案

   public class MainWindowViewModel : ViewModelBase
   {
    public ViewModel1 ViewModel1 { get; set; }
    public ViewModel2 ViewModel2 { get; set; }
    public ViewModel3 Model3 { get; set; }

    public MainWindowViewModel()
    {
        ViewModel1 = new ViewModel1();
        ViewModel2 = new ViewModel2();
        ViewModel3 = new ViewModel3();
        ViewModel1.PropertyChanged += (s,e) => 
        {
           if(e.PropertyName == "IsBusy") 
           { 
              // set the MainWindowViewModel.IsBusy property here
              // for example:
              IsBusy = ViewModel1.IsBusy;
           }
         }
    //IsBusy = true; - its working
   }
}

重复订阅所有 View 模型。

不要忘记取消订阅事件,当您不再需要它时,以避免内存泄漏。

您的问题是:您绑定(bind)到 MainWindowViewModel 的属性,而不是内部 ViewModel 的属性。

关于c# - 使用 MVVM 的 BusyIndi​​cator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12384012/

相关文章:

c# - 使用 C# 在 ASP.NET MVC3 中进行模型绑定(bind)的好资源?

c# - 尝试在任何 CPU 上使用 TaskScheduler 时出现 BadImageFormatException

c# - 将 C# 控制台应用程序转换为服务(Main 方法不起作用)

c# - 深度空检查,有没有更好的方法?

c# - Double 数据类型,计算小数点后的小数位数

c# - WPF AvalonEdit SQL xhsd 请求

c# - 在 WPF .NET Framework 中使用自定义任务管理器时,如何防止进程重复?

c# - WPF - ListView 更新非常零星

wpf - MVVM模式下处理WPF按钮双击

c# - JwtSecurityTokenHandler 返回小写声明类型