WPF组合框验证 [英] WPF Combobox validation

查看:165
本文介绍了WPF组合框验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox with Sex(男性,女性..):我要求用户选择一个值(ComboBox默认没有值。)

 < ComboBox ItemsSource ={x:Static Member = data:Sex.AllTypes}SelectedItem ={Binding Path = Sex.Value,ValidatesOnDataErrors = True,UpdateSourceTrigger = PropertyChanged,NotifyOnValidationError = True}VerticalAlignment =Top> 
< ComboBox.ItemTemplate>
< DataTemplate>
< StackPanel Orientation =Horizo​​ntal>
< TextBlock Text ={Binding Path = Name}/>
< / StackPanel>
< / DataTemplate>
< /ComboBox.ItemTemplate>
< / ComboBox>

Sex.Value是我的Person类中的属性:

  public class Person:IDataErrorInfo 
{
public string this [string columnName]
{
get

switch(columnName)
{
caseSex:return Sex.Value == null? 必填字段:null;
caseSurname:return string.IsNullOrEmpty(Nachname)? 必填字段:null;
}
}
}
public string错误
{
get
{
return null;
}
}
}

问题是输入这个[string columnname]。



当我尝试一个带有名称的TextBox时,它输入这个[string columnname],一切正常:



< TextBox Style ={StaticResource textBoxInError}Text ={Binding Path = Surname,ValidatesOnDataErrors = True,UpdateSourceTrigger = PropertyChanged,NotifyOnValidationError = True} >


解决方案

Windows中的好方法是添加一个值无)到组合框中,并测试该人是否包含(无)值。 (无)是一个元选项,因为它不是选择的有效值,而是描述了选项本身没有被使用。



正确:



不正确:



验证不适用于您的情况,因为当您想说没有选择性别时没有选择值...


I have a ComboBox with Sex(male, female..):And I demand from user to select a value (the ComboBox has no value by default.)

<ComboBox ItemsSource="{x:Static Member=data:Sex.AllTypes}" SelectedItem="{Binding Path=Sex.Value, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"  VerticalAlignment="Top">
        <ComboBox.ItemTemplate>
             <DataTemplate>
                  <StackPanel Orientation="Horizontal">
                      <TextBlock Text="{Binding Path=Name}" />
                  </StackPanel>
             </DataTemplate>
        </ComboBox.ItemTemplate>
</ComboBox>

Sex.Value is a Property in my Person class:

public class Person : IDataErrorInfo 
{
public string this[string columnName]
        {
            get
            {                
                switch (columnName)
                {                   
                    case "Sex": return Sex.Value == null ? "Required field" : null;
                    case "Surname": return string.IsNullOrEmpty(Nachname) ? "Required field" : null;
                }                
            }
        }
 public string Error
        {
            get
            {
                return null;
            }
        }
}

the problem is that it never enters this[string columnname].

When i try a TextBox with name, it enters this[string columnname] and everything works fine:

<TextBox  Style="{StaticResource textBoxInError}"   Text="{Binding Path=Surname, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}"/>

解决方案

The good way in windows is to add a value (None) into the combobox and test if the person contains the (None) value. "(None)" is a meta-option because it is not a valid value for the choice—rather it describes that the option itself isn't being used.

Correct:

Incorrect:

The validation doesn't work in your case because no value is selected when you want to say that no sex is selected...

这篇关于WPF组合框验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆