动态绑定?

时间:2015-06-29 12:27:25

标签: c# wpf

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

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将字段的值绑定到默认模型中的值并且它是正确的,但是当我打开新的View时 我的getter正确返回相应View.Model.TextForValueField的值,但我的Windows功能区菜单上的TextBox字段显示默认模型的初始值(?) 为什么?? 用于绑定的xaml是:

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

也许有一种方法可以在View启动时再次调用getter?像&#34;刷新&#34;用于丝带?

我的属性改变了功能

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

1 个答案:

答案 0 :(得分:1)

当你通过ViewModel属性转发 Model属性时,你必须手动进行更新。

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

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

BindProperty绑定到Model.Property时,Model发生更改时,通知

一种可能的解决方案是INotifyPropertyChanged实施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)); } } )。

$('#new_row').click(function() { //1
    $('#ghate_table').append('<tr><td>' + row + '</td><td><input name="ng_name_' + row + '" type="text" class="ng_name form-control full_w " ></td><td class ="ng_service_td"><div class="selected_container" id="selected_container_' + row + '" ><input name="service_time_' + row + '" id="service_time_' + row + '" type="number" class="service_time form-control " > <select name="service_time_type_' + row + '" class="form-control" ><option>ماه</option><option>روز</option><option> ساعت </option> </select> </div></td><td> <span class = "yes_change"><input name="change_' + row + '" class="change_' + row + '" id="change_yes_' + row + '" type="radio" value="yes"  checked> دارد </span>   <input name="change_time_' + row + '" id="change_time_' + row + '" type="number" class="change_time form-control " >  <select name="change_time_type_' + row + '" class="form-control" ><option>     ماه </option> <option>  روز </option><option>ساعت</option> </select><span class = "no_change"> <input name="change_' + row + '" class="change_' + row + '" id="change_no_' + row + '" type="radio" value="no" > ندارد  </span>  </td> </tr> ');

    specific_row_id[row] = row;
    var change = '.change_' + specific_row_id[row];
    var change_checked = '.change_' + specific_row_id[row] + ':checked'
    var change_time = '#change_time_' + specific_row_id[row];

    alert(specific_row_id[row]);

    $(change).change(function() { //2

        if ($(change_checked).val() == 'yes') {
            $(change_time).prop('required', true);
            $(change_time).prop('readonly', false);
            $(change_time).css('background', '#fff');
        } else {
            $(change_time).prop('readonly', true);
            $(change_time).css('background', 'rgba(255, 117, 117, 0.3)');
            $(change_time).prop('required', false);
        }
    }); //2
    row++;

}); //1