更改绑定列表中的属性后更新对象属性



我对WPF和MVVM很陌生,目前这让我非常沮丧。我目前的问题是:

我有以下 XAML:

<Window.DataContext>
<local:MainViewModel />
</Window.DataContext>
<WrapPanel Orientation="Horizontal">
<ComboBox ItemsSource="{Binding StreetList}" SelectedItem="{Binding SelectedStreet}" DisplayMemberPath="StreetName" IsSynchronizedWithCurrentItem="True"/>
<DataGrid ItemsSource="{Binding SelectedStreet.HouseList}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="YearBuilt" Binding="{Binding YearBuilt}" />
<DataGridTextColumn Header="Price" Binding="{Binding Price}" />
<DataGridTextColumn Header="#Rooms" Binding="{Binding Rooms}" />
</DataGrid.Columns>
</DataGrid>
<TextBox Text="{Binding AvgPricePerRoom, Mode=OneWay}" />
</WrapPanel>

哪里:

public class MainViewModel : ViewModelBase
{
public BindingList<StreetViewModel> StreetList { get { return _streetList; } }
private BindingList<StreetViewModel> _streetList;
public StreetViewModel SelectedStreet
{
get { return _selectedStreet; }
set { _selectedStreet = (StreetViewModel)value.Clone(); RaisePropertyChanged(); RaisePropertyChanged(nameof(AvgPricePerRoom)); }
}
private StreetViewModel _selectedStreet;
public double AvgPricePerRoom
{
get
{
return
_selectedStreet.HouseList[0].Price / _selectedStreet.HouseList[0].Rooms / 3 +
_selectedStreet.HouseList[1].Price / _selectedStreet.HouseList[1].Rooms / 3 +
_selectedStreet.HouseList[2].Price / _selectedStreet.HouseList[2].Rooms / 3;
}
}
public MainViewModel()
{
_streetList = new BindingList<StreetViewModel>();
fill_HouseGroupList();
}
void fill_HouseGroupList() { ... }
}

和(已编辑(:

public class StreetViewModel : ViewModelBase, ICloneable
{
public string StreetName
{
get { return _streetName; }
set { _streetName = value; RaisePropertyChanged(); }
}
private string _streetName;

private BindingList<HouseViewModel> _houseList;
private void HouseList_ListChanged(object sender, ListChangedEventArgs e)
{
throw new NotImplementedException();
}
public StreetViewModel() { }
public StreetViewModel(string streetName) : this()
{
_streetName = streetName;
_houseList = new BindingList<HouseViewModel>();
HouseList.ListChanged += HouseList_ListChanged;
}
public BindingList<HouseViewModel> HouseList
{
get
{
if(_houseList.Count == 0)
{
switch (_streetName)
{
case "Main street":
_houseList.Add(new HouseViewModel(new House(2015, 600, 9)));
_houseList.Add(new HouseViewModel(new House(1929, 20, 4)));
_houseList.Add(new HouseViewModel(new House(1969, 30, 6)));
break;
case "School street":
_houseList.Add(new HouseViewModel(new House(2017, 50, 10)));
_houseList.Add(new HouseViewModel(new House(1930, 10, 5)));
_houseList.Add(new HouseViewModel(new House(1970, 20, 7)));
break;
case "Garden street":
_houseList.Add(new HouseViewModel(new House(2014, 1, 15)));
_houseList.Add(new HouseViewModel(new House(1935, 20, 11)));
_houseList.Add(new HouseViewModel(new House(1978, 20, 9)));
break;
}
}
return _houseList;
}
}
public object Clone()
{
var houseGroupCopy = new StreetViewModel(this._streetName);
houseGroupCopy._houseList = new BindingList<HouseViewModel>();
foreach (var house in _houseList)
{
houseGroupCopy._houseList.Add(new HouseViewModel(new House(house.YearBuilt, house.Price, house.Rooms)));
}
return houseGroupCopy;
}
}

加:

public class HouseViewModel : ViewModelBase
{
public House House
{
get { return _house; }
set { _house = value;  RaisePropertyChanged(); }
}
private House _house;
public int YearBuilt
{
get { return _house.YearBuilt; }
set { _house.YearBuilt = value; RaisePropertyChanged(); }
}
public double Price
{
get { return _house.Price; }
set { _house.Price = value; RaisePropertyChanged(); }
}
public int Rooms
{
get { return _house.Rooms; }
set { _house.Rooms = value; RaisePropertyChanged(); }
}
public HouseViewModel(House house)
{
_house = house;
}
}

。因此,在我的视图的数据网格中,我可以编辑房屋列表中的房屋项目的属性,这是一个绑定列表。 如何使属性 AvgPricePerRoom (显示在文本框中(知道 DataGrid 中的更改,以便在网格中更改时显示更新的 AvgPricePerRoom?我使用了 BindingList,因为我读到与 ObservableCollection 不同,当其项目的属性更改时,它也会引发一个事件(性能问题除外(,但是在此示例中,我如何处理此事件并通知我的 AvgPricePerRoom?

(并非所有上面的代码对我的问题都很重要,对此我很抱歉......

绑定集合时,必须注意 3 种更改通知:

  1. 用于在列表中添加/删除的通知。这是唯一一个通知可观察集合实际处理。
  2. 公开可观察集合的属性的通知(示例中为街道列表(。OC 在大规模更改方面非常糟糕(没有 AddRange 函数(,因此通常最好在代码中创建新列表,仅将其公开为最后一步。
  3. 街景模型每个属性的更改通知。

我找到了一个解决方案,尽管我不确定这是否是"正确的方法":

按照建议,我将AvgPricePerRoom变成了带有支持字段的完整属性,并添加了一个设置AvgPricePerRoom的函数:

void CalculateAverage()
{
AvgPricePerRoom =
_selectedStreet.HouseList[0].Price / _selectedStreet.HouseList[0].Rooms / 3 +
_selectedStreet.HouseList[1].Price / _selectedStreet.HouseList[1].Rooms / 3 +
_selectedStreet.HouseList[2].Price / _selectedStreet.HouseList[2].Rooms / 3;
}

然后我将SelectedStreet属性更改为:

public StreetViewModel SelectedStreet
{
get { return _selectedStreet; }
set
{
_selectedStreet = value.Clone() as StreetViewModel;
CalculateAverage();
_selectedStreet.HouseList.ListChanged += (o, e) => CalculateAverage();
RaisePropertyChanged();
}
}

这是一个合适的 MVVM 解决方案吗?

相关内容

  • 没有找到相关文章

最新更新