我是WPF的新手.在我的UserControl中,我有8个标签及其各自的8个文本框,如下所示:
1.Label : abc 2.Label : def
TextBox1 : TextBox2 :
3.Label :xyz 4. Label : ghi
Textbox3 : TextBox4 :
Run Code Online (Sandbox Code Playgroud)
每个文本框的文本属性应包含文本与其相应的标签名称为结局TextBox1.text应该是xxxx.abc,TextBox2.text应该是xxxx.def等.如果没有文本框应该有红色边框.
希望我清楚细节.所以我需要ValidationRule为每个文本框写不同的??
你输入的任何??
目前我有一个网格,我正在尝试使用具有验证规则的单元格.为了验证它,我需要行的最小值和最大值.
验证类:
public decimal Max { get; set; }
public decimal Min { get; set; }
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var test = i < Min;
var test2 = i > Max;
if (test || test2)
return new ValidationResult(false, String.Format("Fee out of range Min: ${0} Max: ${1}", Min, Max));
else
return new ValidationResult(true, null);
}
Run Code Online (Sandbox Code Playgroud)
用户控制:
<telerik:RadGridView SelectedItem ="{Binding SelectedScript}"
ItemsSource="{Binding ScheduleScripts}">
<telerik:RadGridView.Columns>
<telerik:GridViewDataColumn
DataMemberBinding="{Binding Amount}" Header="Amount"
CellTemplate="{StaticResource AmountDataTemplate}"
CellEditTemplate="{StaticResource AmountDataTemplate}"/>
<telerik:GridViewComboBoxColumn
Header="Fee Type"
Style="{StaticResource FeeTypeScriptStyle}" …Run Code Online (Sandbox Code Playgroud) 由于某种原因,我的程序找不到我的自定义验证规则 f\xc3\xbcr 我的文本框。我定义验证规则所在的命名空间
\n\nxmlns:valRule="clr-namespace:MovieDB.UI.Validation" \nRun Code Online (Sandbox Code Playgroud)\n\n我将我的规则绑定到文本框
\n\n <TextBox x:Name="textBoxName" Height="23" Margin="59,13,10,0" TextWrapping="Wrap" VerticalAlignment="Top">\n <TextBox.Text>\n <Binding Path="archiveName">\n <Binding.ValidationRules>\n <valRule:ArchiveNameValidationRule />\n </Binding.ValidationRules>\n </Binding>\n </TextBox.Text>\n </TextBox>\nRun Code Online (Sandbox Code Playgroud)\n\n我在这里定义我的规则
\n\nnamespace MovieDB.UI.Validation\n{\n public class ArchiveNameValidationRule : ValidationRule\n {\n\n private static char[] FORBIDEN_CHARS = new char[] { \'*\', \'&\', \'#\', \'/\', \' \', \'\\\\\', \'+\', \'=\', \'?\', \')\', \'(\', \']\', \'[\', \'}\',\n \'{\', \'%\', \'$\', \'\xc2\xa7\', \'"\', \'!\', \'\xc3\xb6\', \'\xc3\xbc\' ,\'\xc3\xa4\', \'\xc3\x84\', \'\xc3\x96\', \'\xc3\x9c\', \':\', \'.\', \';\', \',\', };\n\n public override ValidationResult Validate(object …Run Code Online (Sandbox Code Playgroud) 在我目前的项目中,我必须以WPF形式处理数据验证.我的表单位于ResourceDictionnary中的DataTemplate中.我可以通过两个按钮来保存和加载表单中的数据,这两个按钮序列化和反序列化数据(通过两个DelegateCommand).
如果我的表单中的一个字段为空或无效,则禁用保存按钮.由于UpdateSourceTrigger属性,每次更改时都会检查一个字段.这就是为什么我需要在C#代码中知道如果字段无效以更新我的保存命令.
目前,我在我的XAML绑定中使用ExceptionValidationRule,我想知道它是否是一个很好的实践.我无法实现ValidationRule,因为我需要在C#代码中知道字段是否无效,以更新保存命令(启用或禁用保存按钮).
<TextBox>
<Binding Path="Contact.FirstName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<ExceptionValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
在这篇博客中,我们可以阅读:
在Setter中引发异常并不是一个很好的方法,因为这些属性也是由代码设置的,有时可以暂时保留它们的错误值.
我已经阅读过这篇文章,但我不能使用它,我的TextBox在DataTemplate中,我不能在我的C#代码中使用它们.
所以,我想知道我是否应该更改我的数据验证并且不要使用ExceptionValidationRule.