UWP XAML UserControl绑定

时间:2019-01-06 14:15:11

标签: uwp uwp-xaml

我有一个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具有一个设置为Model Person的数据上下文,以及一个我在父ViewModel中处理的依赖项属性OkClicked。单击该按钮时,这将正确触发。如何将整个Person对象放入peopleViewModel Person1属性?

2 个答案:

答案 0 :(得分:1)

在PersonView UserControl中,您可以使用 RoutedEventHandler 而不是ICommand来绑定点击事件。我已经修改了您的代码,只做了很少的更改就可以在PeopleViewModel中获得person1。希望对您有所帮助。

// PersonView.Xaml

<Grid>
   <StackPanel>
      <TextBox Text="{x:Bind person.FirstName,Mode=TwoWay}"  />
      <TextBox Text="{x:Bind person.LastName,Mode=TwoWay}" />
      <Button Click="OnClick" HorizontalAlignment="Center"  Content="OK"></Button>
   </StackPanel>
</Grid> 

// PersonView.Xaml.cs

public sealed partial class PersonView : UserControl
{
    Person person;
    public event RoutedEventHandler Click;
    public PersonView()
    {
       person = new Person();
       this.InitializeComponent();
       this.DataContext = person;
    }
    private void OnClick(object sender, RoutedEventArgs args)
    {
       Click?.Invoke(sender, args);
    }
}

// MainView.Xaml

<Grid>
   <local:PersonView Click="{x:Bind viewModel.PersonOkButtonClicked,Mode=OneWay}">
   </local:PersonView>
</Grid>

// PeopleViewModel

 public class PeopleViewModel 
 {
    public Person Person1 { get; set; }
    public PeopleViewModel()
    {           
    }
    public void PersonOkButtonClicked(object sender,RoutedEventArgs e)
    {
       Person1 = (sender as FrameworkElement).DataContext as Person;
    }
 }

答案 1 :(得分:0)

如果您使用Prism和DelegateCommand并希望使用Dependencyproperties而不是另一个ViewModel来将对象传递给ViewModel,那么我发现您可以执行以下操作。

//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" />

在您的ViewModel中:

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

在viewmodel构造函数中

    myModelOkClick = new DelegateCommand<MyModel>(CustomerDetailsOk);

在viewmodel中,一种用于处理点击和模型的方法。

    private void CustomerDetailsOk(MyModel model)
    {

    }