单个ObservableCollection用于多个DataGrid RowDetailsTemplate

时间:2017-05-23 19:34:03

标签: c# wpf mvvm datagrid wpfdatagrid

我正在尝试将文件列表上传到数据库。加载的工作方式是用户选择一个文件,然后将该文件读入ObservableCollection。数据包括FileMeta信息和基金信息。对于每个文件,如果缺少某些基金信息,则向用户显示该基金以填写缺失的信息。多个文件可能包含缺少信息的同一基金。我在逐个文件的基础上显示缺少的资金信息。我的目标是拥有一个基金模型和多个基金信息显示。当基金信息在一个位置更新时,我希望所有位置都反映出更新。我已经能够在多个地方显示基金信息,但不能在后台显示一个模型。如何在我的所有数据网格后面获得一个模型?

User Interface

XAML

<DataGrid Grid.Row="1" Grid.ColumnSpan="3" Margin="10" ItemsSource="{Binding Path=Files}" SelectedValue="{Binding Path=SelectedUploadFile}" AutoGenerateColumns="False" CanUserAddRows="False">
    <DataGrid.Columns>
        <!--Removed for brevity-->
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>                    
            <DataGrid ItemsSource="{Binding Path=Funds}" CanUserAddRows="False" AutoGenerateColumns="False" VerticalScrollBarVisibility="Visible" MaxHeight="200">                            
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Fund Name">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Label Content="{Binding Path=FundName, Mode=OneWay}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>                            
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
   </DataGrid.RowDetailsTemplate>
</DataGrid>

视图模型

public class AuditTests : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    // Objects
    FileLoader fileLoader = new FileLoader();

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public ObservableCollection<FileMeta> Files { get; set; }

    public AuditTests()
    {
        // Collection Initialization
        InitializeData();
    }

    public void InitializeData()
    {
        Files = new ObservableCollection<FileMeta>() { new FileMeta () };
    }

    private void AddFileRequested()
    {
        Files = new ObservableCollection<FileMeta>() { new FileMeta () };
        SaveFilesCommand.RaiseCanExecuteChanged();
    }
}

FileMeta

public partial class FileMeta : INotifyPropertyChanged
{
    private ObservableCollection<Fund> _funds;

    public event PropertyChangedEventHandler PropertyChanged;

    public FileMeta()
    {
        Funds = new ObservableCollection<Fund>();
    }

    private void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }

    public ObservableCollection<Fund> Funds
    {
        get
        {
            return _funds;
        }
        set
        {
            if (_funds != value)
            {
                _funds = value;
                RaisePropertyChanged("Funds");
            }
        }
    }
}

FileLoader

public class FileLoader
{
    public ObservableCollection<Fund> Funds { get; set; }

    public void LoadFile(FileMeta file)
    {
        newFund = GetFund(row.Cell(1).Value.ToString());
        file.Funds.Add(newFund);
    }

    private Fund GetFund(string fundName)
        {
            using (var db = new IISContext())
            {
                // If it exists in the database
                if ((from f in db.Fund where f.FundName == fundName select f).Count() == 0)
                {
                    // If we've already loaded this time
                    var existingFunds = (from f in Funds where f.FundName == fundName select f);
                    if (existingFunds.Count() == 0)
                    {
                        Fund newFund = new Fund();
                        newFund.FundId = Guid.NewGuid();
                        newFund.Cusip = "";
                        newFund.FundFamilyId = (from ff in db.FundFamily where ff.FundFamilyName == "Default" select ff.FundFamilyId).First();
                        newFund.FundName = fundName;
                        Funds.Add(newFund);
                        return newFund;
                    }
                    else
                    {
                        return existingFunds.First();
                    }
                }
                else
                {
                    return (from f in db.Fund where f.FundName == fundName select f).First();
                }
            }
        }
}

基金

public partial class Fund : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private Guid _fundID;
        private string _cusip;
        private string _fundName;
        private bool _selected;
        private FundFamily _fundFamily;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public Fund()
        {
            PortfolioHolding = new HashSet<PortfolioHolding>();
        }

        public Guid FundId
        {
            get
            {
                return _fundID;
            }
            set
            {
                if (_fundID != value)
                {
                    _fundID = value;
                    RaisePropertyChanged("FundID");
                }
            }
        }

        public string Cusip
        {
            get
            {
                return _cusip;
            }
            set
            {
                if (_cusip != value)
                {
                    _cusip = value;
                    RaisePropertyChanged("Cusip");
                }
            }
        }

        public Guid FundFamilyId { get; set; }

        public string FundName
        {
            get
            {
                return _fundName;
            }
            set
            {
                if (_fundName != value)
                {
                    _fundName = value;
                    RaisePropertyChanged("FundName");
                }
            }
        }

        [NotMapped]
        public bool Selected
        {
            get
            {
                return _selected;
            }
            set
            {
                if (_selected != value)
                {
                    _selected = value;
                    RaisePropertyChanged("Selected");
                }
            }
        }

        public virtual ICollection<PortfolioHolding> PortfolioHolding { get; set; }

        public virtual FundFamily FundFamily
        {
            get
            {
                return _fundFamily;
            }
            set
            {
                if (_fundFamily != value)
                {
                    _fundFamily = value;
                    RaisePropertyChanged("FundFamily");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

听起来你想要一份主要的基金清单。每次从文件加载基金时,首先检查该基金的主列表(假设他们有某种类型的唯一ID;可能是股票代码?)。如果它已经存在,请使用现有实例而不是创建新实例。如果它尚未存在,请创建它并将其添加到主列表中。

要么是这样,要么每次更新基金时,递归搜索您的资金文件集合,其身份与您刚刚更新的资产相同,并更新它们以匹配。

你想要的是这样的:

AuditTests
    Files: [
        FileMeta
            Funds: [ Fund A, Fund C ]

        FileMeta 
            Funds: [ Fund A, Fund B, Fund D ]
    ]

FileLoader
    Funds: [ Fund A, Fund B, Fund C, Fund D ]

“基金A”并不仅仅意味着它们看起来相同,它与实际的Fund对象实例相同。

Fund需要正确实施INotifyPropertyChanged