求WPF 录入为空验证简单代码

山之魂2 2018-06-19 10:26:01
在做一个WPF项目,录入项还没有加验证提示。
求WPF 录入为空显示 红色 * 号的简单代码
...全文
804 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
验证错误提示默认是红色的边框 自定义的话修改它的ErrorTemplate 模板
山之魂2 2018-06-20
  • 打赏
  • 举报
回复
TextBox的按二楼的方法,加一个样式做出来了。 现在还缺Combobox 为空提示的验证,如果有人有可用代码的话给一下,可以另开一贴或者加200分给他,先谢谢了
山之魂2 2018-06-20
  • 打赏
  • 举报
回复
引用 2 楼 ylly11111 的回复:
<Window x:Class="ErrorTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ErrorTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBox HorizontalAlignment="Left"  
                 Height="50"   VerticalAlignment="Top" Width="237">
            <TextBox.Text>
                <Binding Path="FName"  NotifyOnValidationError="True"   
                    UpdateSourceTrigger="PropertyChanged">
                    <Binding.ValidationRules>
                        <local:ValidClass></local:ValidClass>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

    </Grid>
</Window>
添加一个验证文件
  public class ValidClass : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            var a = value.ToString();
            if (string.IsNullOrWhiteSpace(a))
            {
                return new ValidationResult(false, "不可为空");
            }
            else
            {
                return new ValidationResult(true, null);
            }
        }
    }
添加一个ViewModel
    public class VM : INotifyPropertyChanged
    {
 
        private string _FName;

        public string FName
        {
            get { return _FName; }
            set { _FName = value; RaisePropertyChanged("FName"); }
        }
 
        private void RaisePropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this,new PropertyChangedEventArgs (name));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
谢谢,我昨天下午查资料写出来了,跟你这个基本上一样,非常感谢。
ylly11111 2018-06-20
  • 打赏
  • 举报
回复
<Window x:Class="ErrorTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ErrorTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<TextBox HorizontalAlignment="Left"
Height="50" VerticalAlignment="Top" Width="237">
<TextBox.Text>
<Binding Path="FName" NotifyOnValidationError="True"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:ValidClass></local:ValidClass>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>

</Grid>
</Window>

添加一个验证文件
  public class ValidClass : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var a = value.ToString();
if (string.IsNullOrWhiteSpace(a))
{
return new ValidationResult(false, "不可为空");
}
else
{
return new ValidationResult(true, null);
}
}
}

添加一个ViewModel
    public class VM : INotifyPropertyChanged
{

private string _FName;

public string FName
{
get { return _FName; }
set { _FName = value; RaisePropertyChanged("FName"); }
}

private void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this,new PropertyChangedEventArgs (name));
}
public event PropertyChangedEventHandler PropertyChanged;
}

8,756

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧