更新:感謝 Andrew KeepCoding 的 CustomNumberBox 類。這真的很有幫助,并且比其他解決方案更容易使用。它在沒有資料系結的情況下作業。
關于 Chris Schaller 提出并由 Richard Zhang 回答的問題,我有一個澄清/用戶錯誤:如何解決 NumberBox 中忽略的 UpdateSourceTrigger
我收到以下兩個錯誤:
錯誤 CS1061“NumberBox”不包含“VisualTreeFindName”的定義,并且找不到接受“NumberBox”型別的第一個引數的可訪問擴展方法“VisualTreeFindName”(您是否缺少 using 指令或程式集參考?)
錯誤 CS0103 此處的當前背景關系中不存在名稱“模型”
這是上一個答案的擴展(盡管我的是命名空間會計..)
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;
}
}
}
}
uj5u.com熱心網友回復:
您也可以像這樣創建自定義控制元件。這里的一個缺點是它總是作為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;
}
}
}
uj5u.com熱心網友回復:
錯誤 CS1061“NumberBox”不包含“VisualTreeFindName”的定義,并且找不到接受“NumberBox”型別的第一個引數的可訪問擴展方法“VisualTreeFindName”(您是否缺少 using 指令或程式集參考?)
您會收到此錯誤,因為該方法不是通用方法,并且在您呼叫它時VisualTreeFindName不需要。<TextBox>洗掉<TextBox>并將回傳值轉換為 a應該可以TextBox作業,并在.TextBoxNumberBox
if (this.ThisNumberBox.VisualTreeFindName("InputBox") is TextBox inputBox)
{
inputBox.TextChanged = InputBox_TextChanged;
}
錯誤 CS0103 此處的當前背景關系中不存在名稱“模型”
您收到此錯誤是因為您的代碼中不存在模型。此模型只是您用作參考的問題中使用的屬性。用您的目標屬性替換模型應該可以解決問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/513546.html
標籤:C#xmlwinui-3
上一篇:Laravel-爭論太少
下一篇:Android.Util.AndroidRuntimeException'只有創建視圖層次結構的原始執行緒才能接觸其視圖。
