c# - ViewModel 和 Model 之间的 WPF 绑定(bind)

标签 c# wpf mvvm

在对这个问题进行了重大编辑之后,我希望现在已经清楚了。

当 1 个更改应该影响多个属性时,我对 WPF 中的绑定(bind)感到非常困惑。

我经常使用 VVM 将我的 ViewModel 绑定(bind)到我的 View,我会说我可以接受。

我正在尝试实现一个状态 Controller 。这意味着,无论我在 UI 的一部分中进行什么设置,反射都已完成。

例如,在我的 UI 部分,我可以打开或关闭某个功能,例如“显示图像”

当我进行此更改时,我希望我的应用程序中的所有内容都得到通知并采取相应措施。

所以,我的 StateController 类将有一个属性

public bool ShowImages

在我看来,我可能会有类似的东西
<image Visible ="{Binding ShowImages", Converter={StaticConverter ConvertMe}}" />

我遇到的问题是如何让 StateController 向我的所有 ViewModel 发出警报。

目前,在每个 ViewModel 中,我假设我必须重复相同的属性
public bool ShowImages

例如
public class StateController : BaseViewModel
{
    public bool ShowImages{get;set;}//imagine the implementation is here
}

public class ViewModelB : BaseViewModel
{
    public bool ShowImages{}//imagine the implementation is here
}

public class ViewModelB : BaseViewModel
{
    public bool ShowImages{}//imagine the implementation is here
}

所以,我的问题是,如果我更新了 ViewModelB.ShowImages,我将如何首先通知 StateController,而后者又会更新所有 ViewModel。

这是 INotifyPropertyChanged 可以自动为我做的事情,因为它们都共享相同的 propertyName,还是我必须手动实现逻辑,例如
public static class StateController
{
    public bool ShowImages{get;set;}//imagine the implementation is here
}

public class ViewModelA : BaseViewModel
{
    public bool ShowImages
    {
        get { return StateController.ShowImages; }
        set { StateControllerShowImages = value;
              OnPropertyChanged("ShowImages"); }
     }
}

public class ViewModelB : BaseViewModel
{
    public bool ShowImages
    {
        get { return StateController.ShowImages; }
        set { StateControllerShowImages = value;
              OnPropertyChanged("ShowImages"); }
     }

}

我讨厌上述实现的想法,但它确实显示了我想要实现的目标。我只是希望有更好的方法!

最佳答案

仅针对该一个对象模型引发 PropertyChange 通知。

所以提出 "Name" 的更改通知ClassA 的属性(property)只会在绑定(bind)到特定 ClassA.Name 的情况下更新 UI .它不会触发任何 ClassB.Name 的更改通知,或 ClassA.Name 的其他实例.

我建议在这里为您的 StateModel 使用 Singleton ,并让您的其他模型订阅 StateModel.PropertyChanged知道它是否应该更新的事件,例如 this answer .

public ViewModelA
{
    public ViewModelA()
    {
        StateController.Instance.PropertyChanged += StateController_PropertyChanged;
    }

    void StateController_PropertyChanged(object sender, NotifyPropertyChangedEventArgs e)
    {
        // if singleton's ShowImages property changed, raise change 
        // notification for this class's ShowImages property too
        if (e.PropertyName == "ShowImages")
            OnPropertyChanged("ShowImages");
    }

    public bool ShowImages
    {
        get { return StateController.Instance.ShowImages; }
        set { StateController.Instance.ShowImages = value; }
    }
}

关于c# - ViewModel 和 Model 之间的 WPF 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24308478/

相关文章:

c# - 亚马逊 S3 : How to get a list of folders in the bucket?

c# - 通过标签创建许多对象的数组/列表(最初未知数量)

c# - 公开 ViewModel 事件以绑定(bind)到自定义 DependencyProperty

c# - ViewModel 中用于 INotifyPropertyChanged 模型中原始属性的 getter 和 setter

c# - 无法更新按钮单击WPF中的列表

c# - 使用 NotificationBase 的数据绑定(bind)问题

c# - MVVM 使用工厂方法处理窗口管理

c# - int int.operator(int left, int right) &

c# - 如何使用证书签署 ActiveX DLL

wpf - 如何覆盖特定主题中的样式