UWP数据绑定对象到UserControl



我是UWP编程的新手。我想将对象绑定到UserControl(而不仅仅是对象的属性(,这样当我在MainPage中操作MyModel对象时,它就会在UserControl中更新。我认为XAML数据绑定已经从WPF时代发展到现在使用预编译的x:Bind。我读到的例子似乎很复杂,它们混合了不同时代的技术,或者涉及幕后魔术。我想使用x:Bind将数据绑定归结为其要点,尽可能避免MVVM、Collections、Bindings、Datacontext等。当我单击按钮时,要使Usercontrol显示更新的MyProperty,代码中需要的最小更改是什么?

主页.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>

主页.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事件?(

我的"真实"项目显然要复杂得多,所以如果可能的话,我希望坚持当前的项目结构(名称空间(。

提前感谢您的时间和耐心!祝你今天愉快:-(

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

主页.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;
}

最新更新