【问题标题】:UWP Classic Binding not updating UI after setting bound value from Inputcontrol从 Inputcontrol 设置绑定值后,UWP 经典绑定不更新 UI
【发布时间】:2016-12-21 04:17:43
【问题描述】:

为了简化我的问题,在我的应用程序中,我想将用户的输入全部更改为大写。所以当TextBox失去焦点时,“foo”应该显示为“FOO”。

我的 Xaml:

<Page x:Class="App12.MainPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:local="using:App12"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d">
    <Page.DataContext>
        <local:MainViewModel />
    </Page.DataContext>

    <StackPanel Margin="10,50,10,10" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBox Text="{Binding Name1, Mode=TwoWay}" />
        <TextBox Text="{x:Bind Path=vm.Name2, Mode=TwoWay}" />
        <Button HorizontalAlignment="Center">Just a control for the TextBox to lose focus</Button>
    </StackPanel>
</Page>

我的视图模型

using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace App12
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public MainViewModel() 
        {

        }

        private string _name1 = "something";

        public string Name1
        {
            get
            {
                return _name1;
            }
            set
            {
                _name1 = (string)value.ToUpper();
                OnPropertyChanged();
            }
        }

        private string _name2 = "something";

        public string Name2
        {
            get
            {
                return _name2;
            }
            set
            {
                _name2 = (string)value.ToUpper();
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged( [CallerMemberName] string propertyName = null )
        {
            var handler = PropertyChanged;
            handler?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
        }
    }
}

还有我的代码隐藏

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace App12
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        MainViewModel vm;
        public MainPage()
        {
            this.InitializeComponent();
            DataContextChanged += MainPage_DataContextChanged;
        }

        private void MainPage_DataContextChanged( FrameworkElement sender, DataContextChangedEventArgs args )
        {
            vm = (MainViewModel)DataContext;
        }



    }
}

当我在 UWP 应用(第一个文本框)中使用经典绑定时,此代码不起作用

我看到 setter 被调用, OnNotifyPropertyChanged 也被调用,并且处理程序不为空。变量 _text 被分配了它的新值就好了(全部大写),但是我从来没有看到公共变量 Text 的 getter 被调用。 我还尝试了一个转换器(实现了 ConvertBack),结果相同。 但是,使用 x:Bind(第二个文本框),它确实有效。

在 WPF 中,这也可以按预期工作。 我错过了什么还是绑定改变了?根据 Microsoft 告诉我们的内容以及我所看到的内容,它不应该有。

【问题讨论】:

    标签: c# xaml data-binding visual-studio-2015 uwp-xaml


    【解决方案1】:

    我找到了另一个Q/A in Stackoverflow,上面写着:

    这里的问题是UWP中的绑定系统是“智能的”。对于 TwoWay 绑定,对目标的更改将自动传播到源,在这种情况下,绑定系统假定 PropertyChanged 事件将为源中的相应属性触发并忽略这些事件。因此,即使您的源代码中有 RaisePropertyChanged 或 NotifyPropertyChanged,TextBox 仍然不会更新。

    顺便说一句,我不知道如何使用经典的双向绑定来解决此问题。

    【讨论】:

    • 感谢您的回复。我很难相信这是经典绑定中的故意“智能”,因为它没有在 x:Bind 或 WPF 绑定中实现。我认为这是微软应该修复的错误。顺便说一句,我已经在 MSDN 论坛上发布了这个问题,但到目前为止,微软还没有给出任何答案,甚至没有说他们正在研究这个问题。如果这是故意的,我认为这是一个糟糕的变化。这样的事情应该留给知道他们的特定用户要求什么的开发人员。再次感谢!!!
    猜你喜欢
    • 2020-10-27
    • 2015-03-04
    • 2015-12-16
    • 2021-04-06
    • 2020-01-16
    • 2021-10-24
    • 2011-05-15
    • 1970-01-01
    • 2020-06-11
    相关资源
    最近更新 更多