【问题标题】:UWP x:bind two-way bindingUWP x:bind 双向绑定
【发布时间】:2019-08-27 16:06:37
【问题描述】:

目标:使用双向绑定创建两个绑定到同一个对象的文本框,这样如果我在一个中更新文本,我会看到 other 一个自动显示我正在输入的文本,反之亦然。我还希望看到相同的文本出现在文本块中(只读,单向绑定)。我需要使用 x:bind 语法,而不是 Binding 语法

这是我目前所拥有的,但不起作用:

XAML:

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

<StackPanel>
    <TextBox Text="{x:Bind Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox Text="{x:Bind Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <TextBlock Text="{x:Bind Foo, Mode=OneWay}"/>
</StackPanel>

C#

namespace App1
{
    public sealed partial class MainPage : Page
    {
        public string Foo {get; set;}
        public MainPage()
        {
            this.InitializeComponent();
        }

    }

}

【问题讨论】:

  • 如果将 Foo 初始化为一个值,它会在 View 的任何位置设置吗?

标签: c# data-binding uwp


【解决方案1】:

这就是我最终不得不做的事情。没想到双向绑定会带来这么多包袱。

XAML:

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

    <StackPanel>
        <TextBox Text="{x:Bind ViewModel.Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBox Text="{x:Bind ViewModel.Foo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <TextBlock Text="{x:Bind ViewModel.Foo, Mode=OneWay}"/>
    </StackPanel>

</Page>

C#:

 namespace App1
{
    public sealed partial class MainPage : Page
    {
        public TextModel ViewModel { get; set; }

        public MainPage()
        {
            ViewModel = new TextModel();
            this.InitializeComponent();
        }
    }

    public class TextModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string foo = "hello";
        public string Foo
        {
            get => foo;

            set
            {
                foo = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Foo"));
            }
        }

    }

}

【讨论】:

  • 这不是专门的双向绑定问题。 XAML 仅知道某些内容是否已更新(如果它是 DependencyProperty 或您使用 INotifyPropertyChanged 事件)。对于您在初始帖子中的一般属性,它永远不会更新 UI,因为引擎不会发送任何通知。
【解决方案2】:

任何绑定只有在收到更改通知时才会在 UI 中更新,有两种方法可以做到这一点。您需要将 Foo 属性设置为 DependencyProperty 或让您的页面实现 INotifyPropertyChanged 接口(在依赖属性文档中也有调用)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-26
    • 2015-12-11
    • 2016-03-04
    • 2019-10-26
    • 1970-01-01
    • 2018-10-21
    • 2023-03-27
    • 2018-01-09
    相关资源
    最近更新 更多