UWP数据绑定对象到UserControl

时间:2018-12-06 15:57:36

标签: c# xaml uwp

我是UWP编程的新手。我想将一个对象绑定到UserControl(而不仅仅是对象的属性),以便在操作MainPage中的MyModel对象时,可以在UserControl中对其进行更新。 我收集到XAML数据绑定已从WPF时代发展到现在使用预编译的x:Bind。我读过的例子似乎很复杂,是来自不同时代的混合技术,或者涉及幕后魔术。我想使用x:Bind将数据绑定简化为基本要素,并尽可能避免MVVM,Collections,Bindings,Datacontext等。在代码中,当我单击按钮时,使用户控件显示更新的MyProperty所需的最小更改是什么?

MainPage.xaml

<Page
    x:Class="MyApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="using:MyApp.Controls"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Grid>
        <controls:MyUserControl MyModel="{x:Bind MyModel}"/>
        <Button Click="Button_Click"/>
    </Grid>
</Page>

MainPage.xaml.cs

using MyApp.Models;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MyApp
{
     public sealed partial class MainPage : Page
    {
        public MyModel MyModel = new MyModel();
        public MainPage() { this.InitializeComponent(); }
        private void Button_Click(object sender, RoutedEventArgs e)
        { MyModel.MyProperty += 1; }
    }
}

MyModel.cs

namespace MyApp.Models
{
    public class MyModel 
    { public int MyProperty { get; set; } }    
}

MyUserControl.xaml

<UserControl
    x:Class="MyApp.Controls.MyUserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300"
    d:DesignWidth="400">    
   <TextBlock Text="{x:Bind  MyModel.MyProperty}"/>    
</UserControl>

MyUserControl.xaml.cs

using MyApp.Models;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace MyApp.Controls
{
    public sealed partial class MyUserControl : UserControl 
    {
        public MyModel MyModel
        {
            get { return (MyModel)GetValue(MyModelProperty); }
            set { SetValue(MyModelProperty, value); }
        }

        public static readonly DependencyProperty MyModelProperty =
            DependencyProperty.Register("MyModel", typeof(MyModel), typeof(MyUserControl), new PropertyMetadata(0));

          public MyUserControl()
        { this.InitializeComponent(); }       
    }
}

请注意,当我在MainPage构造函数中设置MyModel.MyProperty时,UserControl会显示该值。我尝试在MyModel类中使用INotifypropertyChanged接口,单击按钮时会触发PropertyChanged事件,但UserControl不会更新。 (所以也许我的问题是如何使MyUsercontrol侦听并响应MyModel中的propertyChanged事件?)

我的“真实”项目显然要复杂得多,因此,如果可能的话,我想坚持使用当前的项目结构(命名空间)。

提前感谢您的时间和耐心!祝你有美好的一天:-)

1 个答案:

答案 0 :(得分:0)

这是我在文件中所做的更改。首先将绑定模式更改为TwoWay,然后更改实现 INotifyPropertyChanged 的模型,以触发对UI元素的更改。

Mainpage.xaml

<controls:MyUserControl MyModel="{x:Bind MyModel,Mode=TwoWay}"/>

MyUserControl.xaml

<TextBlock Text="{x:Bind  MyModel.MyProperty,Mode=TwoWay}"/>

MyModel.cs

public class MyModel : INotifyPropertyChanged
    {
        private int _MyProperty;
        public int MyProperty
        {

            get { return _MyProperty; }
            set
            {
                if (value != _MyProperty)
                {
                    _MyProperty = value;
                    NotifyPropertyChanged();
                }
            }
        }
        private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }