我使用MsDesktop Stack示例here来创建我的模式并为实体集建立一个view / viewmodel / repository,其PrimaryKey(称为PartNumber)使用存储过程函数映射映射到ComplexType的许多Attribute属性。
我很容易订阅RaisePropertyChangedEvent(“Parts”)来填充列表框,但是我无法获得:
public Part SelectedPart
{
get { return p_SelectedPart; }
set
{
base.RaisePropertyChangingEvent("SelectedPart");
p_SelectedPart = value;
base.RaisePropertyChangedEvent("SelectedPart");
}
}
当我在OnPropertyChangedEvent中订阅它时:
void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case "SelectedPart":
/* When we select a Part, we need to configure the view model
* to display the selected Part's attributes. */
VMSProcServices.ReturnExtendedPartProperties(this);
break;
case "Parts":
/* When we load a Parts collection from the data store, we replace
* the existing collection in the Parts property. After we do that,
* we need to subscribe to the CollectionChanging event of the new
* collection. */
this.Parts.CollectionChanging += OnPartsCollectionChanging;
break;
}
其中ReturnExtendedProperties()定义为:
public static void ReturnExtendedPartProperties(MainWindowVM mainWindowVM)
{
MyEntities myEntities = new MyEntities();
mainWindow.ReturnAttsPerPn_Result = new ObservableCollection<ReturnAttsPerPn_Result>();
if (mainWindowVM.SelectedPart != null)
{
mainwWindowVm.ReturnAttsPerPn_Result.Clear();
foreach (ReturnAttsPerPn_Result attResult in myEntities.ReturnAttsPerPn(mainWindowVM.SelectedPart.PnID))
{
mainWindowVM.ReturnAttsPerPn_Result.Add(attResult);
}
}
}
调试时,我的Locals窗口中没有SelectedPart属性的数据,但Part对象的ObservableCollection就在那里(这很明显,因为我可以看到这些数据)。此外,我甚至从未介入“SelectedPart”的情况,只在我的OnPropertyChanged方法中使用“Part”。
我的主要问题是,(并且因为我是初学者而缺乏清晰度):我如何有效地“捕获”SelectedPart属性的值并将其提供给我的存储过程,并将输出用作集合?
感谢阅读,非常感谢任何帮助:)
编辑::更改标题
答案 0 :(得分:0)
令人尴尬地发现了我的错误,这只是绑定路径中的语法错误,我没有抓到(并且没有发布。对于任何感兴趣的人,这是一个更清晰的存储过程结果获取方法,可能是对某人有用:
public static void ReturnExtendedPartProperties(MainWindowVM mainWindowVM)
{
mainWindow.ReturnAttsPerPn_Result = new ObservableCollection<ReturnAttsPerPn_Result>();
using (var myEntities = new MyEntities())
{
var results = myEntities.ReturnAttsPerPn(mainWindowVM.SelectedPart.PnID)
if (mainWindowVM.SelectedPart != null)
{
mainWindowVM.ReturnAttsPerPn_Result.Clear();
foreach (ReturnAttsPerPn_Result attresult in results)
{
mainWindowVM.ReturnAttsPerPn_Result.Add(attresult);
}
}
}
}