首页 文章

UWP Classic绑定在从Inputcontrol设置绑定值后未更新UI

提问于
浏览
0

为了简化我的问题,在我的应用程序中,我想将用户的输入更改为全部大写 . 因此,当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>

我的ViewModel

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应用程序(First TextBox)中使用经典绑定时,此代码不起作用

我看到调用setter,OnNotifyPropertyChanged也被调用,处理程序不为null . 变量_text被赋予其新值就好了(全部大写),但后来我从未看到公共变量Text的getter被调用 . 我也尝试了一个转换器(使用ConvertBack实现),结果相同 . 使用x:绑定(第二个TextBox),它确实有效 .

在WPF中,这也按预期工作 . 我错过了什么或者Binding有变化吗?根据微软告诉我们的内容和我所看到的它不应该有的东西 .

1 回答

  • 0

    我发现另一个Q/A in Stackoverflow说:

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

    顺便说一下,我无法弄清楚如何使用经典的TwoWay Binding为这个问题创建一个解决方法 .

相关问题