C# 当文本框被启用时,如何触发新的验证?
在wpf对话框窗口中,我有一个复选框,用于启用和禁用文本框。文本框已将ValidateSondaErrors设置为True。通过IDataErrorInfo,如果复选框已选中,则仅检查此文本框的值 我的问题是,如果用户选中复选框,则不会对文本框执行新的验证,因此我不会得到表示错误的红色框 以下是一个小样本进行演示:C# 当文本框被启用时,如何触发新的验证?,c#,wpf,validation,xaml,C#,Wpf,Validation,Xaml,在wpf对话框窗口中,我有一个复选框,用于启用和禁用文本框。文本框已将ValidateSondaErrors设置为True。通过IDataErrorInfo,如果复选框已选中,则仅检查此文本框的值 我的问题是,如果用户选中复选框,则不会对文本框执行新的验证,因此我不会得到表示错误的红色框 以下是一个小样本进行演示: <Window x:Class="Validation.ValidationWindow" xmlns="http://schemas.microsoft.com/wi
<Window x:Class="Validation.ValidationWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ValidationWindow" Height="300" Width="300">
<DockPanel LastChildFill="False">
<CheckBox DockPanel.Dock="Top" IsChecked="{Binding InputAllowed}">Input allowed</CheckBox>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Label>InputValue</Label>
<TextBox Text="{Binding InputValue, ValidatesOnDataErrors=True}" IsEnabled="{Binding InputAllowed}" Width="50"/>
</StackPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Label>InputValue2</Label>
<TextBox Text="{Binding InputValue2, ValidatesOnDataErrors=True}" Width="50"/>
</StackPanel>
<Button DockPanel.Dock="Bottom" Click="OnOk">Ok</Button>
</DockPanel>
</Window>
允许输入
输入值
输入值2
好啊
代码隐藏:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Validation
{
/// <summary>
/// Interaction logic for ValidationWindow.xaml
/// </summary>
public partial class ValidationWindow : Window, IDataErrorInfo
{
public bool InputAllowed
{
get { return (bool)GetValue(InputAllowedProperty); }
set { SetValue(InputAllowedProperty, value); }
}
public static readonly DependencyProperty InputAllowedProperty =
DependencyProperty.Register("InputAllowed", typeof(bool), typeof(ValidationWindow), new PropertyMetadata(false));
public int InputValue
{
get { return (int)GetValue(InputValueProperty); }
set { SetValue(InputValueProperty, value); }
}
public static readonly DependencyProperty InputValueProperty =
DependencyProperty.Register("InputValue", typeof(int), typeof(ValidationWindow), new PropertyMetadata(0));
public int InputValue2
{
get { return (int)GetValue(InputValue2Property); }
set { SetValue(InputValue2Property, value); }
}
public static readonly DependencyProperty InputValue2Property =
DependencyProperty.Register("InputValue2", typeof(int), typeof(ValidationWindow), new PropertyMetadata(0));
public ValidationWindow()
{
InitializeComponent();
DataContext = this;
}
private void OnOk(object sender, RoutedEventArgs e)
{
string msg = Error;
if(!string.IsNullOrEmpty(Error))
{
MessageBox.Show(Error);
return;
}
DialogResult = true;
}
#region IDataErrorInfo Members
public string Error
{
get { return ((IDataErrorInfo)this)[null]; }
}
public string this[string columnName]
{
get
{
string msg = string.Empty;
if(string.IsNullOrEmpty(columnName))
{
msg += ((IDataErrorInfo)this)["InputValue"];
msg += ((IDataErrorInfo)this)["InputValue2"];
}
else
{
switch(columnName)
{
case "InputValue":
if(InputAllowed)
{
if(InputValue <= 0)
{
msg += "InputValue must be greater that 0!";
}
}
break;
case "InputValue2":
if(InputValue2 <= 0)
{
msg += "InputValue2 must be greater that 0!";
}
break;
}
}
return msg;
}
}
#endregion
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Shapes;
命名空间验证
{
///
///ValidationWindow.xaml的交互逻辑
///
公共部分类验证窗口:窗口,IDataErrorInfo
{
允许公共布尔输入
{
获取{return(bool)GetValue(InputAllowedProperty);}
set{SetValue(InputAllowedProperty,value);}
}
公共静态只读DependencyProperty InputAllowedProperty=
DependencyProperty.Register(“InputAllowed”、typeof(bool)、typeof(ValidationWindow)、new PropertyMetadata(false));
公共整数输入值
{
获取{return(int)GetValue(InputValueProperty);}
set{SetValue(InputValueProperty,value);}
}
公共静态只读从属属性InputValueProperty=
DependencyProperty.Register(“InputValue”、typeof(int)、typeof(ValidationWindow)、new PropertyMetadata(0));
公共整数输入值2
{
get{return(int)GetValue(InputValue2Property);}
设置{SetValue(InputValue2Property,value);}
}
公共静态只读从属属性InputValue2Property=
DependencyProperty.Register(“InputValue2”、typeof(int)、typeof(ValidationWindow)、new PropertyMetadata(0));
公共验证窗口()
{
初始化组件();
DataContext=this;
}
私有void OnOk(对象发送方,路由目标)
{
字符串msg=错误;
如果(!string.IsNullOrEmpty(错误))
{
MessageBox.Show(错误);
返回;
}
DialogResult=true;
}
#区域IDataErrorInfo成员
公共字符串错误
{
获取{return((IDataErrorInfo)this)[null];}
}
公共字符串此[string columnName]
{
得到
{
string msg=string.Empty;
if(string.IsNullOrEmpty(columnName))
{
msg+=((IDataErrorInfo)this)[“InputValue”];
msg+=((IDataErrorInfo)this)[“InputValue2”];
}
其他的
{
开关(列名称)
{
案例“InputValue”:
如果(输入允许)
{
如果(InputValue我们一直在使用它在程序上更改文本后强制验证,如果您调用它来响应复选框的事件,它也应该起作用:
var binding = someTextBox.GetBindingExpression( TextBox.TextProperty );
if( binding == null )
return;
binding.UpdateSource();
我们一直在使用此功能在程序更改文本后强制验证,如果您在响应复选框事件时调用此功能,也应该有效:
var binding = someTextBox.GetBindingExpression( TextBox.TextProperty );
if( binding == null )
return;
binding.UpdateSource();
我们一直在使用此功能在程序更改文本后强制验证,如果您在响应复选框事件时调用此功能,也应该有效:
var binding = someTextBox.GetBindingExpression( TextBox.TextProperty );
if( binding == null )
return;
binding.UpdateSource();
我们一直在使用此功能在程序更改文本后强制验证,如果您在响应复选框事件时调用此功能,也应该有效:
var binding = someTextBox.GetBindingExpression( TextBox.TextProperty );
if( binding == null )
return;
binding.UpdateSource();
与stijn的解决方案没有实际区别,但由于我们都有点懒惰:
public static class DependencyPropertyExtensions
{
public static bool UpdateSource(this FrameworkElement source, DependencyProperty property)
{
var binding = source.GetBindingExpression(property);
if (binding != null)
{
binding.UpdateSource();
return true;
}
return false;
}
}
与stijn的解决方案没有实际区别,但由于我们都有点懒惰:
public static class DependencyPropertyExtensions
{
public static bool UpdateSource(this FrameworkElement source, DependencyProperty property)
{
var binding = source.GetBindingExpression(property);
if (binding != null)
{
binding.UpdateSource();
return true;
}
return false;
}
}
与stijn的解决方案没有实际区别,但由于我们都有点懒惰:
public static class DependencyPropertyExtensions
{
public static bool UpdateSource(this FrameworkElement source, DependencyProperty property)
{
var binding = source.GetBindingExpression(property);
if (binding != null)
{
binding.UpdateSource();
return true;
}
return false;
}
}
与stijn的解决方案没有实际区别,但由于我们都有点懒惰:
public static class DependencyPropertyExtensions
{
public static bool UpdateSource(this FrameworkElement source, DependencyProperty property)
{
var binding = source.GetBindingExpression(property);
if (binding != null)
{
binding.UpdateSource();
return true;
}
return false;
}
}
我尝试了你的代码,验证工作与我完美-无论是在复选框打开还是关闭的情况下,如果输入不是一个数字,我都会得到红色边框。@tube builder它对我不起作用。此外,这里的条件是否不是一个数字,当它不是正数时,它被认为是无效的。尝试选中复选框,你会看到第一个文本框是有效的没有任何红色边框,但单击按钮会正确显示错误消息。我们必须更改文本框的值(例如删除当前的0值并重新键入该值)为了让它正常工作。我尝试了你的代码,验证工作与我完美地结合在一起-无论是在复选框打开还是关闭的情况下,如果输入不是一个数字,我都会得到红色边框。@tube builder它对我不起作用。此外,这里的条件是否不是一个数字,当它不是正时,它被认为是无效的。尝试选中复选框,你会看到第一个文本框没有任何红色边框,尽管单击按钮会正确显示错误消息。我们必须更改文本框的值(例如删除当前的0值并重新键入该值)为了让它正常工作。我尝试了你的代码,验证工作与我完美地结合在一起-无论是在复选框打开还是关闭的情况下,如果输入不是一个数字,我都会得到红色边框。@tube builder它对我不起作用。此外,这里的条件是否不是一个数字,当它不是正时,它被认为是无效的。尝试选中复选框,你会看到第一个文本框没有任何红色边框,尽管单击按钮会正确显示错误消息。我们必须更改文本框的值(例如删除当前的0值并重新键入该值)我尝试了你的代码,验证工作非常完美——无论是打开还是关闭复选框,如果输入不是数字,我都会得到红色边框