在没有INotifyPropertyChanged的情况下更新ObservableCollection成员属性的绑定

时间:2015-03-30 07:01:16

标签: c# .net wpf mvvm data-binding

我有一个WPF View绑定到实现ViewModel的{​​{1}}。在INotifyPropertyChanged中,我有一个View,其中的项目绑定到DataGrid中的ObservableCollectionViewModel成员本身未实施ObservableCollection。在INotifyPropertyChanged我有三列 - 两个可编辑的DataGrid列和一个只读DatePicker列,当用户选择包含当前日期的日期范围时,应该检查这些列。

首次加载视图时绑定有效,但是当我更改其他两列中的日期时,Checkbox的绑定未更新,可能是因为类Checkbox未实现{ {1}}。我真的不想在这里实现该接口,因为MyDomainObject是从Web服务返回的类型,这样做会迫使我创建和维护该类型的副本,我有一个我的应用程序中有很多类似的场景。有什么办法可以强制Checkbox更新吗?如果可能的话,我非常希望避免使用代码 - 如果我必须打破这个规则,我知道该怎么做。

以下代码应大致说明问题:

XAML:

INotifyPropertyChanged

查看型号:

ObservableCollection

域对象:

<DataGrid ItemsSource="{Binding MyDomainObjectCollection}">
    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Start Date">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding StartDate, StringFormat=d}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <DatePicker SelectedDate="{Binding StartDate, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="End Date">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding EndDate, StringFormat=d}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <DatePicker SelectedDate="{Binding EndDate, UpdateSourceTrigger=PropertyChanged}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

        <DataGridTemplateColumn Header="Is Active">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsActive, Mode=OneWay}" IsEnabled="False" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

    </DataGrid.Columns>
</DataGrid>

2 个答案:

答案 0 :(得分:1)

如果您不想在域对象上实现INotifyPropertyChanged接口,则可以使用与DomainObject具有相同接口并实现INotifyPropertyChanged

的ViewModel包装DomainObject。
public class MyDomainObjectViewModel : INotifyPropertyChanged {

  private MyDomainObject _domainObject;

  public MyDomainObjectViewModel(MyDomainObject domainObject){
    _domainObject = domainObject;
  }

  public DateTime StartDate { 
    get{
      return _domainObject.StartDate;
    }
    set{
      _domainObject.StartDate = value;
      RaisePropertyChanged("StartDate");
      RaisePropertyChanged("IsActive");
    }
  }

  public DateTime EndDate { 
    get{
      return _domainObject.EndDate ;
    }
    set{
      _domainObject.EndDate = value;
      RaisePropertyChanged("EndDate");
      RaisePropertyChanged("IsActive");
    }
  }

  public bool IsActive {
    get { return StartDate < DateTime.Now && EndDate > DateTime.Now; }
  }

}

您的ObservableCollection的类型为ObservableCollection<MyDomainObjectViewModel>

答案 1 :(得分:1)

几年前我遇到了同样的问题。我找到的解决方案使用动态对象作为实现INotifyPropertyChanged的代理对象。它通过反射挂钩到getter和setter,并在代理更改时更新源对象。

public class DynamicProxy : DynamicObject, INotifyPropertyChanged
{
    private readonly PropertyDescriptorCollection mPropertyDescriptors;


    protected PropertyInfo GetPropertyInfo(string propertyName)
    {
        return
            ProxiedObject.GetType()
                .GetProperties()
                .FirstOrDefault(propertyInfo => propertyInfo.Name == propertyName);
    }

    protected virtual void SetMember(string propertyName, object value)
    {
        var propertyInfo = GetPropertyInfo(propertyName);

        if (propertyInfo.PropertyType == value.GetType())
        {
            propertyInfo.SetValue(ProxiedObject, value, null);
        }
        else
        {
            var underlyingType = Nullable.GetUnderlyingType(propertyInfo.PropertyType);

            if (underlyingType != null)
            {
                var propertyDescriptor = mPropertyDescriptors.Find(propertyName, false);

                var converter = propertyDescriptor.Converter;
                if (converter != null && converter.CanConvertFrom(typeof (string)))
                {
                    var convertedValue = converter.ConvertFrom(value);
                    propertyInfo.SetValue(ProxiedObject, convertedValue, null);
                }
            }
        }
        RaisePropertyChanged(propertyName);
    }

    protected virtual object GetMember(string propertyName)
    {
        return GetPropertyInfo(propertyName).GetValue(ProxiedObject, null);
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    protected virtual void RaisePropertyChanged(string propertyName)
    {
        OnPropertyChanged(propertyName);
    }

    #region constructor

    public DynamicProxy()
    {
    }

    public DynamicProxy(object proxiedObject)
    {
        ProxiedObject = proxiedObject;
        mPropertyDescriptors = TypeDescriptor.GetProperties(ProxiedObject.GetType());
    }

    #endregion

    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        if (binder.Type == typeof (INotifyPropertyChanged))
        {
            result = this;
            return true;
        }

        if (ProxiedObject != null && binder.Type.IsInstanceOfType(ProxiedObject))
        {
            result = ProxiedObject;
            return true;
        }
        return base.TryConvert(binder, out result);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        result = GetMember(binder.Name);
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        SetMember(binder.Name, value);
        return true;
    }

    #region public properties

    public object ProxiedObject { get; set; }

    #endregion

    #region INotifyPropertyChanged Member

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

假设我们有一个像这样的域对象:

public class DomainObject
{
    public string Name { get; set; }
}

这个观点:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox 
        VerticalAlignment="Top"
        Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}">            
    </TextBox>
</Grid>
</Window>

并像这样设置代理:

DataContext = new DynamicProxy(new DomainObject());