UWP XAML 用户控件绑定 [英] UWP XAML UserControl Binding

查看:28
本文介绍了UWP XAML 用户控件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UWP XAML 用户控件,它可能包含在另一个用户控件中,也可能不包含.

I have a UWP XAML user control which might be contained in another user control or might not.

<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 视图

My parent View XAML View

<Grid>
      <local:PersonView OkClicked="{x:Bind viewModel.PersonOkClicked}"> 
      </local:PersonView>
</Grid>

最后是我的 peopleViewModel:

lastly my 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 和我在父 ViewModel 中处理的依赖属性 OkClicked.单击按钮时,这是正确触发的.如何将整个 Person 对象放入 peopleViewModel Person1 属性?

The PersonView has a datacontext set to the model Person and a dependency property OkClicked which I handle in the parent ViewModel. This is firing correctly when the button is clicked. How do I get the whole Person object into the peopleViewModel Person1 property?

推荐答案

如果您使用 Prism 和 DelegateCommand 并且想要使用 Dependencyproperties 而不是另一个视图模型将对象传递给视图模型,我发现您可以做些什么.在您的代码后面:

What I found you can do if your using Prism and DelegateCommand and you want to pass an object to a viewmodel using Dependencyproperties and not another viewmodel.In your code behind:

//Code behind    

public ICommand okCommand
{
    get => (ICommand)GetValue(_okCommand);
    set => SetValue(_okCommand, value);
}
public static readonly DependencyProperty _okCommand = DependencyProperty.Register("okCommand", typeof(ICommand), typeof(CustomerDetailsView), new PropertyMetadata(null));

    public MyModel myModel { get; set; }

    public UserControlView()
    {
        InitializeComponent();
        myModel = new myModel();
        DataContext = myModel;
    }

在 XAML 中

<Button
    Grid.Column="0"
    Command="{x:Bind okCommand}" CommandParameter="{x:Bind myModel}"
    Content="OK" />

在您的视图模型中:

    public DelegateCommand<MyModel> myModelOkClick
    {
        get => _customerDetailsOkClick;
        set => SetProperty(ref _myModelOkClick, value);
    }

在视图模型构造函数中

    myModelOkClick = new DelegateCommand<MyModel>(CustomerDetailsOk);

在视图模型中处理点击和模型的方法.

In viewmodel a method to handle the click and Model.

    private void CustomerDetailsOk(MyModel model)
    {

    }

这篇关于UWP XAML 用户控件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆