WinUI-3 和 C# <> 有关以下内容的说明:如何解决 NumberBox 中忽略的 UpdateSourceTrigger



UPDATE:感谢Andrew为CustomNumberBox类提供KeepCoding。这真的很有帮助,而且比其他解决方案更容易使用。它在没有数据绑定的情况下工作。

关于Chris Schaller提出并由Richard Zhang回答的问题,我有一个澄清/用户错误:如何处理NumberBox 中忽略的UpdateSourceTrigger

我收到以下两个错误:

错误CS1061"NumberBox"不包含"VisualTreeFindName"的定义,并且找不到接受"NumberBox"类型的第一个参数的可访问扩展方法"VisualTreeFindName"(是否缺少using指令或程序集引用?(

错误CS0103当前上下文中不存在名称"Model"此处

这是上一个答案的扩展(尽管我的是名称空间核算。(

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using System;
namespace Accounting
{
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;
}
}
}

这是MainWindow.xaml

<Window
x:Class="Accounting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Accounting"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<NumberBox
x:Name="myNumberBox"
SpinButtonPlacementMode="Compact"
Loaded="NumberBox_Loaded"
Value="0" />
</StackPanel>
</Window>

这是MainWindow.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.Linq;
using System.Reflection; 
namespace Accounting
{
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
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;
}
}
}
}

您也可以创建这样的自定义控件。这里的一个缺点是它将始终作为UpdateSourceTrigger=PropertyChanged工作。

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System.Collections.Generic;
using System.Linq;
namespace NumberBoxes;
public class CustomNumberBox : NumberBox
{
public CustomNumberBox()
{
Loaded += CustomNumberBox_Loaded;
}
private static IEnumerable<T> FindChildrenOfType<T>(DependencyObject parent) where T : DependencyObject
{
if (parent is ContentControl contentControl)
{
if (contentControl.Content is T tContent)
{
yield return tContent;
}
if (contentControl.Content is DependencyObject dependencyObjectContent)
{
foreach (T grandChild in FindChildrenOfType<T>(dependencyObjectContent))
{
yield return grandChild;
}
}
}
else
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T tChild)
{
yield return tChild;
}
foreach (T grandChild in FindChildrenOfType<T>(child))
{
yield return grandChild;
}
}
}
}
private void CustomNumberBox_Loaded(object sender, RoutedEventArgs e)
{
if (FindChildrenOfType<TextBox>(this)
.Where(x => x.Name is "InputBox")
.FirstOrDefault() is TextBox inputBox)
{
inputBox.TextChanged += InputBox_TextChanged;
}
}
private void InputBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender is TextBox inputBox)
{
Text = inputBox.Text;
}
}
}

错误CS1061"NumberBox"不包含"VisualTreeFindName"的定义,并且找不到接受"NumberBox"类型的第一个参数的可访问扩展方法"VisualTreeFindName"(是否缺少using指令或程序集引用?(

您会出现此错误,因为方法VisualTreeFindName不是泛型方法,并且在调用它时不需要<TextBox>。删除<TextBox>并将返回强制转换为TextBox应该可以工作,并在NumberBox中为您提供名为InputBoxTextBox

if (this.ThisNumberBox.VisualTreeFindName("InputBox") is TextBox inputBox)
{
inputBox.TextChanged += InputBox_TextChanged;
}

错误CS0103此处的当前上下文中不存在名称"Model">

您收到此错误是因为代码中不存在模型。此模型只是您用作参考的问题中使用的属性。将模型替换为目标属性就可以了。

最新更新