c# - 动态绑定(bind)?

标签 c# wpf

可能不是问题的正确标题,但我会尝试解释: 我的 TextBox 已经绑定(bind)到如下属性:

class MyViewModel : INotifyPropertyChanged
    public string TextVal
    {
        get
        {
             if (View != null)
                  { 
                         return View.Model.TextForValueField;   // other satrts
                   }
                 return defaultModel.TextForValueField;// first start

        set
        {
             .....//some setters logic with View.Model.TextForValueField
        } 
    }

当程序启动时, View 未打开,因此 getter 将字段的值绑定(bind)到默认模型中的值并且它是正确的,但是当我打开新的 View 时 我的 getter 正确地从相应的 View.Model.TextForValueField 返回值,但是我的 Windows 功能区菜单上的 TextBox 字段显示默认模型的初始值(?) 为什么?? 用于绑定(bind)的 xaml 是:

<Setter Property="Text" Value="{Binding MyViewModel.TextVal, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

也许有一种方法可以在 View 启动时再次调用 getter?丝带的“刷新”之类的东西??

我的属性改变了功能

protected void RaisePropertyChanged(string propName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propName));
            }
        }

最佳答案

当您通过 ViewModel 属性转发模型属性时,您必须手动进行更新。

public class Model
{
    public string Property {get; set;}
}

public class ViewModel
{
    public string BindProperty
    {
        get { return modelInstance.Property; }
        set
        {
            modelInstance.Property = value;
            OnPropertyChanged();
        }
    }
}

当您绑定(bind)到 BindProperty 时,当 Model.Property 发生更改时,您的 View 不会通知

一个可能的解决方案是让 Model 也实现 INotifyPropertyChanged 并转发该通知。另一种(不建议)直接绑定(bind)到 Model.Property(模型仍然必须实现 INotifyPropertyChanged)。

public class Model: NotifyPropertyChangedBase
{
    private string _property;
    public string Property
    {
        get { return _property; }
        set
        {
            _property = value;
            OnPropertyChanged();
        }
    }
}

public class ViewModel: NotifyPropertyChangedBase
{
    public string BindProperty
    {
        get { return modelInstance.Property; }
        set
        {
            modelInstance.Property = value;
            OnPropertyChanged();
        }
    }

    public ViewModel()
    {
        modelInstance.PropertyChanged += Model_PropertyChanged;
    }

    void Model_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        // forward change notification to View
        if(e.PropertyName == "Property")
            OnPropertyChanged("BindProperty");
    }
}

public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged([CallerMemberName] string property = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
}

关于c# - 动态绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31115714/

相关文章:

c# - SSRS : Get list of all reports and parameters in a single web service call?

c# - iOS - 从 Xcode 发布 iOS 应用程序时出错

c# - 亚马逊 API 将订单标记为已发货

wpf - 附加属性

wpf - 条件绑定(bind)属性不起作用?

C# 从调用线程外部的静态类访问函数

c# - ComboBox 中的 ComboBox Item 被选中时的事件

wpf - 删除WPF ListView的“经典”缩进边框

c# - WPF 应用程序在第一次交互后停止/卡住,例如单击按钮

c# - Azure Signalr +Azure function + wpf 客户端 - 如何使用 azure function 和自托管 Azure SignalR App 拥有 wpf 客户端应用程序