更改BindingList中的属性后更新对象属性

时间:2018-05-14 21:04:31

标签: c# wpf mvvm observablecollection bindinglist

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

...所以在我的视图的DataGrid中,我可以编辑HouseList中House项的属性,这是一个BindingList。 如何使属性AvgPricePerRoom(显示在TextBox中)知道DataGrid中的更改,以便在网格更改时显示更新的AvgPricePerRoom?我使用了BindingList,因为我读到它与ObservableCollection不同,它也会在项目属性发生变化时抛出一个事件(性能问题除外),但在这个例子中如何处理这个事件并通知我的AvgPricePerRoom?

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

2 个答案:

答案 0 :(得分:1)

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

  1. 从列表中添加/删除的通知。这是唯一的通知ObservableCollection实际上需要关注。
  2. 公开ObservableCollection的属性的通知(示例中为StreetList)。 OC在大规模更改时非常糟糕(没有AddRange函数),因此通常最好在代码中创建一个新列表,只将它作为最后一步公开。
  3. 更改StreetViewModel每个媒体资源的通知。

答案 1 :(得分:0)

我找到了一个解决方案,虽然我不确定这是否是“正确的方法”:

根据建议,我将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解决方案吗?