2
無論如何,當用戶嘗試鍵入比TextBox.MaxLength屬性允許的更多字符時,發出可見和可聽警告嗎?WPF TextBox MaxLength警告
無論如何,當用戶嘗試鍵入比TextBox.MaxLength屬性允許的更多字符時,發出可見和可聽警告嗎?WPF TextBox MaxLength警告
您可以在綁定上添加ValidationRule。如果驗證失敗,默認ErrorTemplate將用於文本框,否則你也可以自定義...的ValidatonRule的
例如:
class MaxLengthValidator : ValidationRule
{
public MaxLengthValidator()
{
}
public int MaxLength
{
get;
set;
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value.ToString().Length <= MaxLength)
{
return new ValidationResult(true, null);
}
else
{
//Here you can also play the sound...
return new ValidationResult(false, "too long");
}
}
}
以及如何將其添加到綁定:
<TextBlock x:Name="target" />
<TextBox Height="23" Name="textBox1" Width="120">
<TextBox.Text>
<Binding Mode="OneWayToSource" ElementName="target" Path="Text" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:MaxLengthValidator MaxLength="10" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
而這種解決方案實際上並不看TextBox.MaxLength財產(我們使用,在運行時動態生成)它提示的問題,請問有效性規則失敗觸發聲音反饋? – 2011-06-15 07:28:46
有沒有辦法與文本框的MaxLength屬性一起使用? – 2011-06-15 07:29:45
通常不會觸發聲音,但您可以在代碼中看到lilne「//您也可以在此播放聲音...」。在那裏你可以播放你想要的任何聲音。 – fixagon 2011-06-15 07:32:38