我正在尝试将文件列表上载到数据库。加载的工作方式是用户选择一个文件,然后将该文件读取到ObservableCollection中。数据由FileMeta信息和基金信息组成。对于每个文件,如果缺少一些基金信息,则向用户显示基金以填充缺少的信息。多个文件可能包含缺少信息的同一基金。我正在逐个文件地显示缺少的基金信息。我的目标是有一个基金模型和基金信息的多个显示。当基金信息在一个位置更新时,我希望该更新反映在所有位置。我已经能够在多个地方显示基金信息,但不能在背景中显示一个模型。我如何才能让一个模型隐藏在我所有的数据网格后面?

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>ViewModel
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");
}
}
}
}发布于 2017-05-24 03:47:51
听起来你想要一份基金清单。每次你从一个文件中加载一个基金时,首先检查该基金的主列表(假设他们有一个唯一的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。
https://stackoverflow.com/questions/44143583
复制相似问题