samedi 9 mai 2015

Binding To Navigation property doesn't refresh UI

I have this entity (BaseModel Implements INotifypropertyChanged):

EDIT 1: SetProperty code:

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; }

} 

With this navigation property:

/// 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); } 
    }
}

If user changes IntIdBodega property at the View model

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;
            }
        }
    }

In the view model I update the navigation property, But Datagrid never update the values Binded to this property.

    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();  
        }   
    }
}

This is how I bind.

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

My problem is that this column never refresh If i change IsReadOnly to false, the changes can be reflected only if I press F2 at this column.

My question is:

How to force the UI to refresh this navigation properties ?

Aucun commentaire:

Enregistrer un commentaire