绑定到导航属性不会刷新UI

时间:2015-05-08 16:07:38

标签: c# wpf entity-framework

我有这个实体(BaseModel Implements INotifypropertyChanged):

编辑1: SetProperty代码:

protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
{
    if (object.Equals(storage, value)) return false;
       storage = value;

    this.OnPropertyChanged(propertyName);
    return true;
}


public partial class Movimientos : BaseModel
{
    public Movimientos()
    {
    }
    private Nullable<int> _intIdBodega;
    public Nullable<int> IntIdBodega 
    { 
        get { return _intIdBodega; } 
        set { SetProperty(ref _intIdBodega, value); } 
    } 

    public virtual INV_Bodegas INV_Bodegas { get; set; }

} 

使用此导航属性:

/// Nav property 
public partial class INV_Bodegas : BaseModel
{
    public INV_Bodegas()
    {
    }     
    private int _intIdBodega;
    public int IntIdBodega 
    { 
        get { return _intIdBodega; } 
        set { SetProperty(ref _intIdBodega, value); } 
    }

    private string _strCodigoBodega;
    public string StrCodigoBodega 
    { 
        get { return _strCodigoBodega; } 
        set { SetProperty(ref _strCodigoBodega, value); } 
    }
}

如果用户在View模型中更改IntIdBodega属性

public class VieModel
{
    ObservableCollection<Movimientos> mov {get;set;}

    public VieModel()
    {
        mov = new ObservableCollection<Movimientos>();
        mov.CollecTionChanged += mov_CollectionChanged;
    } 

    void mov_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {

            foreach (var item in e.NewItems)
            {
                ((Movimientos)item).PropertyChanged += det_PropertyChanged;
            }
        }
    }

在视图模型中,我更新了导航属性,但Datagrid从不更新Binded到此属性的值。

    public void det_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        Movimientos Idet = (Movimientos)sender;
        // Here I Update The navigation property.
        // But DataGrid never refresh the changes 
        if (e.PorpertyName == "IntIdBodega")
        {
           Idet.INV_Bodegas = db.INV_Bodegas
              .Where(x=> x.IntIdbodega == Idet.Intidbodega)
              .FirstOrDefault();  
        }   
    }
}

这就是我的约束方式。

<DataGridTextColumn Header="{x:Static resources:Labels.Bodega}" 
                                Width="0.5*"
                                Binding="{Binding Inv_Bodegas.StrCodigoBodega,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
                                IsReadOnly="True">

我的问题是这个列永远不会刷新如果我将IsReadOnly更改为false,只有在此栏中按F2时才会反映更改。

我的问题是:

如何强制UI刷新此导航属性?

1 个答案:

答案 0 :(得分:1)

您已实现了界面,但从未引发过该事件。尝试:

public string StrCodigoBodega 
{ 
    get { return _strCodigoBodega; } 
    set {
        SetProperty(ref _strCodigoBodega, value);
        this.PropertyChanged(this, new PropertyChangedEventArgs("StrCodigoBodega"));
    } 
}

参见示例:

https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged%28v=vs.100%29.aspx