[英]Master Detail Binding in WP7
I am having trouble with Binding in WP7 and cannot find an example or blog that will help. 我在WP7中绑定时遇到问题,找不到可以帮助您的示例或博客。 My application has a ViewModel and when navigating from a main page (list) to an edit/detail page I have issues with displaying a ListPicker and Object data correctly binded on the same page. 我的应用程序具有ViewModel,当从主页(列表)导航到编辑/详细信息页面时,我在显示正确绑定到同一页面上的ListPicker和Object数据时遇到问题。 I have managed to make this work, but wish to find the best practise for this. 我设法完成了这项工作,但希望找到最佳实践。
For example I have a class: 例如我有一个课:
public class Person
{
public string Name { get; set; }
public string Gender { get; set; }
}
The class is also referenced in my viewmodel: 我的视图模型中也引用了该类:
public Person selectedPerson;
private Person _person
{
get { return _person; }
set
{
_person= value;
NotifyPropertyChanged("selectedPerson");
}
}
I have a Detail.xaml page: 我有一个Detail.xaml页面:
<StackPanel x:Name="PersonEditPanel">
<TextBox x:Name="NameTextBox" Text="{Binding Name}" />
<TextBox x:Name="GenderTextBox" Text="{Binding Gender}" />
</Stackpanel>
And In my code behind, I have: 在后面的代码中,我有:
private void PhoneApplicationPage_Loaded(object sender, EventArgs e)
{
DataContext = App.ViewModel.selectedPerson;
}
The above is fine, but I would like to add a ListPicker for the Gender attribute to improve user experience. 上面的方法很好,但是我想为Gender属性添加一个ListPicker以改善用户体验。
To achieve this, I add a new class: 为此,我添加了一个新类:
public class TypeList
{
public int ID { get; set; }
public string Name { get; set; }
}
And add an observable collection to my ViewModel: 并将一个可观察的集合添加到我的ViewModel中:
private ObservableCollection<TypeList> _PersonGenderTypeList;
public ObservableCollection<TypeList> PersonGenderTypeList
{
get { return _PersonGenderTypeList; }
set
{
_PersonGenderTypeList = value;
NotifyPropertyChanged("PersonGenderTypeList");
}
}
I have tried many options and the only way I can find where I am able to see the correctly binded Person Name and the Gender list pick and populated on screen (at the same time) is as below: 我尝试了许多选项,唯一可以找到在其中可以看到正确绑定的“人名”和“性别”列表并在屏幕上(同时)填充的性别的方法如下:
private void PhoneApplicationPage_Loaded(object sender, EventArgs e)
{
DataContext = App.ViewModel.selectedPerson;
GenderTypeListPicker.ItemsSource = App.ViewModel.PersonGenderTypeList;
}
This feels like a real hack and probably not best practise. 这感觉像是一个真正的hack,可能不是最佳实践。 Am I missing something here and if so is there a better way to achieve this? 我是否在这里缺少任何东西,如果可以的话,有没有更好的方法来实现这一目标?
I would not class your code as a hack, it is simple and does the job. 我不会将您的代码归类为hack,这很简单并且可以完成工作。 You could make use of the MVVM pattern and create a view model which 'backs' the page, containing both the selected person and the genders: 您可以利用MVVM模式并创建一个视图模型,以“备份”页面,其中包含所选人员和性别:
public PersonDetailsViewModel
{
public Person SelectedPerson {get;set;}
public ObservableCollection<TypeList> GenderList {get;set;}
}
You can then set the instance of this view model as the DataContext
of your view, then bind the various UI controls in XAML. 然后,您可以将此视图模型的实例设置为视图的DataContext
,然后在XAML中绑定各种UI控件。
However, as I said before, there is nothing strictly wrong with your current code! 但是,正如我之前所说,您当前的代码完全没有错!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.