[英]MVVM Subclass property binding
I have issue binding class chain my listView won't refresh data: here 我在绑定类链时遇到问题,我的listView无法刷新数据:这里
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; 因此,当我第一次设置Dealprice并将其添加到集合中时,它就完成了工作,并且listview显示了价值;
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) 问题是当我想要稍后设置NewPosition.Selltrade.Dealprice时,GUI不会显示更改(它在控制台中工作)
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. 这是因为NewPosition and Trade不像您的View模型那样实现INotifyProperyChange 。 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. 对于要获取的更改,您将必须删除并再次添加NewPosition,因为ObservableCollection确实会通知任何更改。
You could look at MvvmLite for a simple framework to help you. 您可以查看MvvmLite寻求帮助的简单框架。
Example using ObservableObject from MvvmLite, which implements INotifyPropertyChanged for you: 使用MvvmLite中的ObservableObject的示例,该示例为您实现INotifyPropertyChanged:
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.