简体   繁体   中英

MVVM Subclass property binding

I have issue binding class chain my listView won't refresh data: here

simplified code:

  private ObservableCollection<NewPosition> _ListPosition;
      public ObservableCollection<NewPosition> ListPosition
    {
                        get { return _ListPosition; }
                       set
                        {

                           _ListPosition = value;
                           OnPropertyChanged("ListPosition");

                        }
                    }

       public class NewPosition
               { 
              public int ID { get; set; }
               public PosStatus Status { get; set; }
                public Trade buyTrade { get; set; }
               public Trade SellTrade { get; set; }

             }

       public class Trade 
      {
                  public double DealPrice {get ; set}

      }

now the view

<ListView Grid.Row="1" Background="#FF232426" ItemsSource="{Binding 
 assetsPointCollection.ListPosition}"
     Foreground="#FFF2F3F7" Margin="0,10" FontSize="10" >
     <ListView.View>
         <GridView> 
<GridViewColumn  Header="Price" Width="80" DisplayMemberBinding="{Binding buyTrade.DealPrice}" />
     <GridViewColumn  Header="SellPrice" Width="80"
     DisplayMemberBinding="{Binding SellTrade.DealPrice}" />  
                             </GridView>
                        </ListView.View>
                    </ListView>
               </Grid>

So whenn i first set dealprice and add it to collection it does the job and listview show value;

 NewPosition thisPos = new NewPosition();
   var trade = new Trade()
                {                   
                    DealPrice = 1000,
                 }
  thisPos.buyTrade = trade;
   ListPosition.Add(thisPos);

the problem is when i want later set NewPosition.Selltrade.Dealprice the GUI will no show change (it work in console)

 pos.SellTrade = new Trade{DealPrice = 1200};

so what is the "elegante manner to implement such code?

It is because NewPosition and Trade does not implement INotifyProperyChange , like your View Model does. The windowing system does not compare the object values, and relies on something telling it a property has changed.

For the change to be picked up, you would have to remove, and add your NewPosition again, since the ObservableCollection does notify any changes.

You could look at MvvmLite for a simple framework to help you.

Example using ObservableObject from MvvmLite, which implements INotifyPropertyChanged for you:

public class NewPosition : ObservableObject
{ 
    private int _id;
    public int ID {
        get => _id;
        set => Set(ref _id, value); 
    }

    private PosStatus _status;
    public PosStatus Status {
        get => _status;
        set => Set(ref _status, value);
    }

    private Trade _buyTrade;
    public Trade buyTrade { 
        get => _buyTrade;
        set => Set(ref _buyTrade, value);
    }

    private Trade _sellTrade;
    public Trade SellTrade { 
        get => _sellTrade;
        set => Set(ref _sellTrade, value);
    }
}

public class Trade : ObservableObject
{
    private double _dealPrice;
    public double DealPrice {
        get => _dealPrice;
        set => Set(ref _dealPrice, value);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM