[英]How do you bind to a complex object in usercontrol.resources for Windows Phone 8.1 (C#)
I have searched the web for the last few days but can't seem to find something that I would have thought was quite a simple task. 我在网上搜索了最近几天,但似乎找不到我认为很简单的内容。 I would like to add a resource in my XAML page of my windows phone application which will reference a complex object but I can't find the correct method. 我想在Windows Phone应用程序的XAML页面中添加资源,该资源将引用复杂的对象,但是我找不到正确的方法。 Is this possible? 这可能吗? Object is made up something similar to: 对象由类似于以下内容的内容组成:
Public class ComplexClass
{
Public string name { get; set; }
Public int ID { get; set; }
Public observablecollection<SimpleClass> simpleObjects { get; set; }
Public addSimpleObject(SimpleClass newSimpleObject)
{
if (simpleObjects == null)
simpleObjects = new ObservableCollection<SimpleClass>();
simpleObjects.Add(newSimpleObject);
}
}
Public Class SimpleClass
{
Public String Name { get; set; }
Public String Disc { get; set; }
}
You could use MVVM do achieve this. 您可以使用MVVM做到这一点。 There are already heaps of tutorials available that you can access to show you how to follow this design pattern, so I won't go into that. 您已经可以访问大量的教程,以向您展示如何遵循这种设计模式,因此我不再赘述。
Instead I'll just show you a simple way of getting the data to your view. 相反,我仅向您展示一种将数据获取到视图的简单方法。
In the constructor of your UserControl (or Page or whatever), set up the DataContext to an instance of your ComplexClass: 在UserControl(或Page或其他控件)的构造函数中,将DataContext设置为ComplexClass的实例:
ComplexClass complexClass;
public MyUserControl1()
{
complexClass = new ComplexClass();
complexClass.AddSimpleObject(new SimpleClass { Name = "Bob" });
this.DataContext = complexClass;
this.InitializeComponent();
}
Then in your XAML you can bind to it like this: 然后在您的XAML中,您可以像这样绑定它:
<StackPanel>
<!-- Binding to properties on ComplexClass -->
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding ID}" />
<ListView ItemsSource="{Binding SimpleObjects}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<!-- Binding to properties on SimpleClass -->
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Disc}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
Without knowing specifics of your code, it's hard for me to suggest a method that is most suitable for you. 在不了解代码细节的情况下,我很难提出最适合您的方法。 I'd read up on MVVM and view models. 我会阅读MVVM并查看模型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.