[英]Restricting Input in WPF
我正在創建一個自定義控件,該控件在XAML中被調用時可以設置為僅允許某些類型的輸入:
<lib:CustomControl RestrictTo="UnsignedIntegersOnly" ... ></CustomControl>
其中UnsignedIntegersOnly是包含允許限制集的Enum的一部分。
如果用戶輸入了不允許的內容,控件將拋出驗證錯誤,並且不允許他繼續下一個表單/頁面/等。
我實現此目標的願景是,在組成此控件的基礎TextBox中,將其文本字段綁定到驗證規則,該驗證規則將作為輸入傳遞,在CustomControl XAML聲明中指定RestrictTo值。 然后,在該ValidationRule類中,處理RestrictTo特定的驗證,並返回驗證是否成功。
這是我不太確定如何進行的地方。 甚至有可能以這種看似動態的方式將參數傳遞給ValidationRule嗎? 我正在設置控件的屬性RestrictTo,然后將其傳遞給它的驗證。
如果有可能,將如何進行? 我應該使用哪種類型的綁定或資源鏈接?
您可能對使用MaskedTextBox控件感興趣,它將限制用戶可以在TextBox中輸入的內容。
由於Microsoft沒有WPF的官方控制權,因此我建議Xceed提供以下內容:
MaskedTextBox (免費使用:-)
在這里,您具有mask的語法:
http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
Title="MainWindow" Height="350" Width="525">
<Grid>
<xctk:MaskedTextBox Mask="0000"></xctk:MaskedTextBox>
</Grid>
</Window>
如果您擁有Visual Studio 2012,則可以通過NuGet輕松獲取此軟件包:
(右鍵單擊您的項目)
注意:在回答上一個問題時,我在發布的鏈接中廣泛使用了驗證規則,但我想說,如果有時候您可以通過精心設計的組件/控件來避免使用驗證規則,那么這樣做是明智的。 正如@Barn在上一個問題中所指出的那樣,通用驗證規則可能很難做,而且有點可疑,因為您必須處理其中的所有類型,因此IMO有點違反直覺,因為驗證器和轉換器通常專門反對通才; 您可能會浪費更多的時間,而不是值得的,並且重用性可能會比您想象的要少。 (來源:我的經驗)
以下代碼可以幫助您入門。 它是一個用戶控件,而不是一個自定義控件。 我建議您先讓代碼作為用戶控件工作,然后再將其轉換為自定義控件。 將有效屬性綁定到視圖模型中控制用戶工作流的某些屬性。
XAML:
<UserControl x:Class="WpfApplication.ValidatingControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" >
<StackPanel Orientation="Horizontal">
<TextBox Name="_textBox" TextChanged="OnTextChanged" Background="LightGray" Width="200"/>
<TextBlock Name="_messageText" Foreground="Red" />
</StackPanel>
</UserControl>
后面的代碼:
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication
{
public partial class ValidatingControl : UserControl
{
public ValidatingControl()
{
InitializeComponent();
}
public enum Restrictions
{
UnsignedIntegersOnly,
SmallIntegersOnly
}
public static readonly DependencyProperty RestrictToProperty =
DependencyProperty.Register("RestrictTo", typeof(Restrictions), typeof(ValidatingControl), new PropertyMetadata(Restrictions.UnsignedIntegersOnly));
public Restrictions RestrictTo
{
get { return (Restrictions)GetValue(RestrictToProperty); }
set { SetValue(RestrictToProperty, value); }
}
public bool Valid
{
get { return (bool)GetValue(ValidProperty); }
set { SetValue(ValidProperty, value); }
}
public static readonly DependencyProperty ValidProperty =
DependencyProperty.Register("Valid", typeof(bool), typeof(ValidatingControl), new UIPropertyMetadata(false));
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
ValidateText(RestrictTo, _textBox.Text);
}
private void ValidateText(Restrictions restrictTo, string text)
{
// validate text, update _messageText, update Valid
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.