如何解决在 NumberBox 中忽略的 UpdateSourceTrigger
How to work around UpdateSourceTrigger ignored in NumberBox
我已尝试在 UWP 应用程序的数据绑定 NumberBox 上将 UpdateSourceTrigger
设置为 PropertyChanged
。
我的数据表单范例是不显示 保存 按钮,直到有数据更改才能实际保存,但是
由于数据源在焦点离开控件之前不会更新,因此保存按钮在用户首先移动到不同的输入控件之后才可用。
- 用户无法单击保存按钮,因为它已被禁用,因此不会触发焦点更改。
这是最小的示例,如果您使用微调器更改值,按钮可用,如果您只在控件中键入内容,则必须单击按钮两次,一次启用它(提交值更改)然后再一次,现在按钮可用。
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
中的内容进行一些判断。
在control style definition of NumberBox中,主体是TextBox
。如果你想干预这个过程,你需要得到这个TextBox
.
1.视觉方法定义
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;
}
}
我已尝试在 UWP 应用程序的数据绑定 NumberBox 上将 UpdateSourceTrigger
设置为 PropertyChanged
。
我的数据表单范例是不显示 保存 按钮,直到有数据更改才能实际保存,但是 由于数据源在焦点离开控件之前不会更新,因此保存按钮在用户首先移动到不同的输入控件之后才可用。
- 用户无法单击保存按钮,因为它已被禁用,因此不会触发焦点更改。
这是最小的示例,如果您使用微调器更改值,按钮可用,如果您只在控件中键入内容,则必须单击按钮两次,一次启用它(提交值更改)然后再一次,现在按钮可用。
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
中的内容进行一些判断。
在control style definition of NumberBox中,主体是TextBox
。如果你想干预这个过程,你需要得到这个TextBox
.
1.视觉方法定义
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;
}
}