C# 如何访问Mainpage.xaml.cs中ItemViewModel类中定义的属性
我有MedicicineModel(MainViewModel类)和MedicineData(ItemViewModel类)。我在MedicineData类中定义了三个属性,如下所示:C# 如何访问Mainpage.xaml.cs中ItemViewModel类中定义的属性,c#,wpf,xaml,windows-phone-8,C#,Wpf,Xaml,Windows Phone 8,我有MedicicineModel(MainViewModel类)和MedicineData(ItemViewModel类)。我在MedicineData类中定义了三个属性,如下所示: namespace MedicinePlus.ViewModels { public class MedicineData { public string ProblemName { get; set; } public string ProblemDesc { g
namespace MedicinePlus.ViewModels
{
public class MedicineData
{
public string ProblemName { get; set; }
public string ProblemDesc { get; set; }
public string ProblemImageFilePath { get; set; }
}
}
namespace MedicinePlus.ViewModels
{
public class MedicineModel
{
public List<MedicineData> Problems { get; set; }
public MedicineModel()
{
this.Problems = new List<MedicineData>();
}
public bool IsDataLoaded { get; set; }
public void LoadData()
{
//place design time datat here
IsDataLoaded = true;
this.Problems.Add(new MedicineData() { ID = 0, ProblemName = "Fever", ProblemDesc = "rise in body temperature", ProblemImageFilePath = "/Assets/Images/fever.png" });
this.Problems.Add(new MedicineData() { ID = 2, ProblemName = "sprain", ProblemDesc = "Caused due to muscle pull", ProblemImageFilePath = "/Assets/Images/sprain1.png" });
this.Problems.Add(new MedicineData() { ID = 3, ProblemName = "bruise", ProblemDesc = "irritation in the concerned area", ProblemImageFilePath = "/Assets/Images/headache.png" });
}
}
}
namespace MedicinePlus
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
// Sample code to localize the ApplicationBar
BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
private void submitMenu_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
MedicineModel类别如下所示:
namespace MedicinePlus.ViewModels
{
public class MedicineData
{
public string ProblemName { get; set; }
public string ProblemDesc { get; set; }
public string ProblemImageFilePath { get; set; }
}
}
namespace MedicinePlus.ViewModels
{
public class MedicineModel
{
public List<MedicineData> Problems { get; set; }
public MedicineModel()
{
this.Problems = new List<MedicineData>();
}
public bool IsDataLoaded { get; set; }
public void LoadData()
{
//place design time datat here
IsDataLoaded = true;
this.Problems.Add(new MedicineData() { ID = 0, ProblemName = "Fever", ProblemDesc = "rise in body temperature", ProblemImageFilePath = "/Assets/Images/fever.png" });
this.Problems.Add(new MedicineData() { ID = 2, ProblemName = "sprain", ProblemDesc = "Caused due to muscle pull", ProblemImageFilePath = "/Assets/Images/sprain1.png" });
this.Problems.Add(new MedicineData() { ID = 3, ProblemName = "bruise", ProblemDesc = "irritation in the concerned area", ProblemImageFilePath = "/Assets/Images/headache.png" });
}
}
}
namespace MedicinePlus
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
// Set the data context of the listbox control to the sample data
DataContext = App.ViewModel;
// Sample code to localize the ApplicationBar
BuildLocalizedApplicationBar();
}
// Load data for the ViewModel Items
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (!App.ViewModel.IsDataLoaded)
{
App.ViewModel.LoadData();
}
}
private void submitMenu_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
查询:现在我想访问submit\u Click事件处理程序代码块中的ProblemName、ProblemDesc和ProblemImgPath。如何操作您必须创建
MedicineData的实例
MedicineData medicineData = new medicineData();
然后,您可以通过以下简单方式访问它们:
medicineData.ProblemName
medicineData.ProblemDesc
及
你们并没有提供足够的信息让我们做出明确的回答,所以我们只能猜测。例如,
App.ViewModel
属性中的对象是什么?它是MedicineModel
实例吗?如果是,那么你似乎忽略了这个属性。您当然可以使用它访问视图模型及其数据吗
private void submitMenu_Click(object sender, EventArgs e)
{
MedicineModel viewModel = App.ViewModel;
List<MedicineData> problems = viewModel.Problems;
string problemName = problems[0].ProblemName;
}
private void submitMenu\u单击(对象发送者,事件参数e)
{
MedicineModel viewModel=App.viewModel;
列出问题=viewModel.problems;
字符串problemName=问题[0]。problemName;
}
如果这不是你想要的,那么也许你可以更详细地告诉我们你想要什么以及你遇到了什么问题。buddy learn数据绑定你的ViewModel与View绑定了吗。或者您只是访问那些没有绑定的问题?请检查我编辑的问题最后缺少的部分来解决问题,发布您的XAML以便我们可以看到您将
submitMenu\u单击
event handler放置在何处快速无效的答案是“不要”。您将数据从视图中分离是有原因的,因此请坚持这样做,而是在视图模型中而不是视图中处理您的单击。使用ICommand
界面。是的,app.viewmodel包含MeicineModel类的实例。那么我不能这样访问它:string problemName=App.ViewModel.Problems[0]。problemName;是的,但实际上,您应该首先检查集合是否包含任何项:if(problems.Count>1).