IDataErrorInfo的 - 没有看到,即使人会拿起任何错误信息 [英] IDataErrorInfo - not seeing any error message even though one gets picked up

查看:345
本文介绍了IDataErrorInfo的 - 没有看到,即使人会拿起任何错误信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ItemType实现一切人会需要验证与IDataErrorInfo的界面的帮助:

 #区域IDataErrorInfo的实施
        // WPF并不需要这一个
        公共字符串错误
        {{返回NULL; }}        公共字符串此[字符串propertyName的]
        {
            {返回GetValidationError(propertyName的); }
        }
        #endregion        #区域验证
        公共BOOL的IsValid
        {
            得到
            {
                的foreach(在ValidatedProperties字符串属性)
                {
                    如果(GetValidationError(财产)!= NULL)
                    {
                        返回false;
                    }
                }                返回true;
            }
        }        静态只读的String [] = ValidatedProperties
        {
            名称
        };        私人字符串GetValidationError(字符串propertyName的)
        {
            如果(Array.IndexOf(ValidatedProperties,propertyName的)小于0)
                返回null;            字符串错误= NULL;            开关(propertyName的)
            {
                案件的Name:
                    错误= ValidateName();
                    打破;
                默认:
                    Debug.Fail(意外财产上的客户正在验证:+ propertyName的);
                    打破;
            }            返回错误;
        }        串ValidateName()
        {
            如果(!IsStringMissing(姓名))
            {
                返回名称不能为空!;
            }            返回null;
        }        静态布尔IsStringMissing(字符串值)
        {
            返回string.IsNullOrEmpty(值)||
                   value.Trim()==的String.Empty;
        }
        #endregion

ItemType的是包裹着ItemViewModel。在ItemViewModel我有当用户点击保存按钮的命令:

 公共ICommand的SaveItemType
        {
            得到
            {
                如果(saveItemType == NULL)
                {
                    saveItemType =新RelayCommand(()=>保存());
                }                返回saveItemType;
            }
        }

接着,在DetailsView,我有以下的XAML code:

 < TextBlock的文本=名称:/>
<文本框Grid.Column =1NAME =NameTextBox文本={绑定名称,ValidatesOnDataErrors = TRUE,UpdateSourceTrigger =的PropertyChanged}
                         Validation.ErrorTemplate ={X:空}/><内容presenter Grid.Row =13Grid.Column =2
                  CONTENT ={绑定的ElementName = NameTextBox,路径=(Validation.Errors).CurrentItem}/>

以下架构回事(这是不明确的,但形式实际上是一个独立的XAML文件(用户控制),其中形式网格的DataContext设置到的ObservableCollection):

 <电网的DataContext ={绑定表项}>
            < Grid.ColumnDefinitions>
                < ColumnDefinition />
                < ColumnDefinition />
            < /Grid.ColumnDefinitions>
            < Grid.RowDefinitions>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
                < RowDefinition高度=自动/>
            < /Grid.RowDefinitions>

这是我遇到的问题是,错误消息没有显示出来。当我断点并检查它是否正确验证,如果我有任何错误消息,当时我有他们。但不知何故,该错误信息不会在XAML到达。

什么是拼图缺少一块?

编辑 - THE MISSING PIECE

对,所以,这是什么是以下内容:

我实现IDataErrorInfo的在我的模型,但不能在一个封装模型视图模型。我所要做的就是,以及是实施视图模型的IDataErrorInfo的接口,并从模型得到它。

IDataErrorInfo的视图模型的实现:

  {{返回(的ItemType为IDataErrorInfo的).Error; }}公共字符串此[字符串propertyName的]
{
  得到
  {
    返回(如的ItemType IDataErrorInfo的)[propertyName的];
  }
}


解决方案

我使用follwoing风格看阉发生与否我的验证。

 <风格X:键={X:TextBox类型}的TargetType ={X:TextBox类型}>
    < Style.Triggers>>
        <触发属性=Validation.HasErrorVALUE =真正的>
            < setter属性=工具提示VALUE ={绑定路径=(Validation.Errors).CurrentItem.ErrorContent,的RelativeSource = {X:静态RelativeSource.Self}}/>
            < setter属性=背景VALUE ={StaticResource的BrushErrorLight}/>
        < /触发>
    < /Style.Triggers>
< /样式和GT;

您应该看到validationmessage在提示

编辑:

尝试NotifyOnValidationError = TRUE添加到您的结合

 <文本框Grid.Column =1NAME =NameTextBox
         文本={绑定名称,ValidatesOnDataErrors = TRUE,NotifyOnValidationError = TRUE,UpdateSourceTrigger =的PropertyChanged}/>

I have ItemType that implements everything one would need for Validation with the help of IDataErrorInfo interface:

#region IDataErrorInfo implementation
        //WPF doesn't need this one
        public string Error
        { get { return null; } }

        public string this[string propertyName]
        {
            get { return GetValidationError(propertyName); }
        }
        #endregion

        #region Validation
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                {
                    if (GetValidationError(property) != null)
                    {
                        return false;
                    }
                }

                return true;
            }
        }

        static readonly string[] ValidatedProperties =
        {
            "Name"
        };

        private string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = ValidateName();
                    break;
                default:
                    Debug.Fail("Unexpected property being validated on Customer: " + propertyName);
                    break;
            }

            return error;
        }

        string ValidateName()
        {
            if (!IsStringMissing(Name))
            {
                return "Name can not be empty!";
            }

            return null;
        }

        static bool IsStringMissing(string value)
        {
            return string.IsNullOrEmpty(value) ||
                   value.Trim() == String.Empty;
        }
        #endregion

ItemType is wrapped with ItemViewModel. On the ItemViewModel I have a command for when a user clicks the Save Button:

public ICommand SaveItemType
        {
            get
            {
                if (saveItemType == null)
                {
                    saveItemType = new RelayCommand(() => Save());
                }

                return saveItemType;
            }
        }

Then, in the DetailsView, I have the following xaml code:

<TextBlock Text="Name:" />
<TextBox Grid.Column="1" Name="NameTextBox" Text="{Binding Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                         Validation.ErrorTemplate="{x:Null}" />

<ContentPresenter Grid.Row="13" Grid.Column="2"
                  Content="{Binding ElementName=NameTextBox, Path=(Validation.Errors).CurrentItem}" />

the following architecture going on (it is not clear, but the form is actually an independent xaml file (User Control), where the datacontext of the grid in the form is set to the ObservableCollection):

<Grid DataContext="{Binding Items}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

The problem that I am having is that error messages are not showing up. When I breakpoint and check if it validates correctly and if I have any error messages, then I do have them. But somehow the error message does not arrive in the xaml.

What is the missing piece of the puzzle?

EDIT - THE MISSING PIECE

Right, so, what it was was the following:

I implemented IDataErrorInfo on my model, but not on the ViewModel that wraps the model. What I had to do was as well was implement the IDataErrorInfo interface on the ViewModel, and get it from the model.

ViewModel Implementation of IDataErrorInfo:

{ get { return (ItemType as IDataErrorInfo).Error; } }

public string this[string propertyName]
{
  get
  {
    return (ItemType as IDataErrorInfo)[propertyName];
  }
}

解决方案

i use the follwoing style to see wether my validation occurs or not.

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
    <Style.Triggers>>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors).CurrentItem.ErrorContent, RelativeSource={x:Static RelativeSource.Self}}"/>
            <Setter Property="Background" Value="{StaticResource BrushErrorLight}" />
        </Trigger>
    </Style.Triggers>
</Style>

you should see the validationmessage in your tooltip

EDIT:

try to add NotifyOnValidationError=true to your binding

<TextBox Grid.Column="1" Name="NameTextBox" 
         Text="{Binding Name, ValidatesOnDataErrors=True, NotifyOnValidationError=true, UpdateSourceTrigger=PropertyChanged}" />

这篇关于IDataErrorInfo的 - 没有看到,即使人会拿起任何错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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