如何解决NumberBox中被忽略的UpdateSourceTrigger [英] How to work around UpdateSourceTrigger ignored in NumberBox

查看:49
本文介绍了如何解决NumberBox中被忽略的UpdateSourceTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将绑定到UpdateSourceTrigger 设置为 PropertyChanged UWP应用程序中的uwp/design/controls-and-patterns/number-box"rel =" nofollow noreferrer> NumberBox .

I have tried to set UpdateSourceTrigger to PropertyChanged on a data bound NumberBox in a UWP application.

我的数据格式范例是在实际更改数据之前不显示 Save 按钮,但是由于直到焦点离开控件后才更新数据源,因此只有在用户首先移至另一个输入控件之后,保存按钮才可用.

My data form paradigm is to not show the Save button until there is a data change to actually save, but as the data source is not updated until focus leaves the control, the save button does not become available until after the user moves to a different input control first.

  • 用户无法单击保存"按钮,因为该按钮已被禁用,因此不会触发焦点更改.

这是最小的示例,如果您使用微调器更改值,则该按钮可用;如果仅键入控件,则必须单击两次该按钮,一次才能启用它(以提交更改值)现在,按钮又可以使用了.

This is the minimal example, if you use the spinners to change the value, the button is available, if you only type into the control you have to click the button twice, once to enable it (to commit the value change) and then once again, now that the button is available.

如果 UpdateSourceTrigger 不起作用,如何提交更改,以便用户在应该可用时第一次单击按钮,因此在更改焦点之前?

If UpdateSourceTrigger is not working, how can commit the change so the user can click the button the first time when it should be available, so before focus change?

<Page
    x:Class="App3.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App3"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:muxc="using:Microsoft.UI.Xaml.Controls"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.DataContext>
        <local:DataClass/>
    </Page.DataContext>
    <Grid>
        <StackPanel>
            <muxc:NumberBox Value="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SpinButtonPlacementMode="Inline"/>
            <Button IsEnabled="{Binding NotZero, Mode=OneWay}" Click="Button_Click">Click if Not Zero</Button>
        </StackPanel>
    </Grid>
</Page>

using System;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

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

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

        public DataClass Model { get => this.DataContext as DataClass; }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var msg = new Windows.UI.Popups.MessageDialog($"Value: {Model.Value}");
            await msg.ShowAsync();
        }
    }

    public class DataClass : INotifyPropertyChanged
    {
        public double Value
        {
            get => _v;
            set
            {
                _v = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NotZero)));
            }
        }
        private double _v;
        public bool NotZero { get => _v != 0; }
        public event PropertyChangedEventHandler PropertyChanged;
    }  
}

推荐答案

这是 NumberBox 的默认行为,通常用于对 NumberBox 中的内容进行判断.输入完成后输入代码.

This is the default behavior of NumberBox, which is usually used to make some judgments on the content in NumberBox after input is completed.

控件样式定义中NumberBox ,主体是 TextBox .如果要干预此过程,则需要获取此 TextBox .

In the control style definition of NumberBox, the main body is TextBox. If you want to intervene in this process, you need to get this TextBox.

1.定义的Viusal方法

public static class StaticExtension
{
    public static FrameworkElement VisualTreeFindName(this DependencyObject element, string name)
    {
        if (element == null || string.IsNullOrWhiteSpace(name))
        {
            return null;
        }
        if (name.Equals((element as FrameworkElement)?.Name, StringComparison.OrdinalIgnoreCase))
        {
            return element as FrameworkElement;
        }
        var childCount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < childCount; i++)
        {
            var result = VisualTreeHelper.GetChild(element, i).VisualTreeFindName(name);
            if (result != null)
            {
                return result;
            }
        }
        return null;
    }
}

2.附加TextChanged处理程序

Xaml

<muxc:NumberBox ... Loaded="NumberBox_Loaded"/>

Xaml.cs

private void NumberBox_Loaded(object sender, RoutedEventArgs e)
{
    var box = sender as NumberBox;
    var textBox = box.VisualTreeFindName<TextBox>("InputBox");
    textBox.TextChanged += TextBox_TextChanged;
}

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    string text = (sender as TextBox).Text;
    bool isNumber = !text.Any(t => !char.IsDigit(t));
    if (isNumber)
    {
        double.TryParse(text, out double value);
        if (value != Model.Value)
            Model.Value = value;
    }
}

这篇关于如何解决NumberBox中被忽略的UpdateSourceTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆