【发布时间】:2019-01-06 14:15:10
【问题描述】:
我有一个 UWP XAML 用户控件,它可能包含在另一个用户控件中,也可能不包含。
<Grid>
<StackPanel >
<TextBox Text="{x:Bind person.FirstName}" />
<TextBox Text="{x:Bind person.LastName}" />
<Button Command="{x:Bind OkClicked}" HorizontalAlignment="Center" Content="OK"></Button>
</StackPanel>
</Grid>
背后的代码:
public sealed partial class PersonView : UserControl
{
Person person => DataContext as Person;
public ICommand OkClicked
{
get { return (ICommand)GetValue(okClicked); }
set { SetValue(okClicked, value); }
}
// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty okClicked =
DependencyProperty.Register("oKclicked", typeof(int), typeof(UserControl), new PropertyMetadata(null));
public PersonView()
{
this.InitializeComponent();
}
}
还有我的班级:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
我的父视图 XAML 视图
<Grid>
<local:PersonView OkClicked="{x:Bind viewModel.PersonOkClicked}">
</local:PersonView>
</Grid>
最后是我的 peopleViewModel:
public class PeopleViewModel : ViewModelBase
{
private DelegateCommand _personOkClicked;
public Person Person1 { get; set; }
public DelegateCommand PersonOkClicked { get => _personOkClicked; set => SetProperty(ref _personOkClicked, value); }
public PeopleViewModel()
{
PersonOkClicked = new DelegateCommand(PersonOkButtonClicked);
}
private void PersonOkButtonClicked()
{
// do something with person1
}
}
PersonView 有一个数据上下文设置为模型 Person 和一个依赖属性 OkClicked,我在父 ViewModel 中处理它。单击按钮时,这会正确触发。如何将整个 Person 对象放入 peopleViewModel Person1 属性中?
【问题讨论】: