Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have tabcontrol in wpf page and in the second tab I have usercontrol. But I using the save viewmodel to both. For validation I used IDataErrorInfo in viewmodel on button click. Validation works for in 1st tab. But in user control, validation works and red border shows around the control but didn't show the error message.

How do I show erro message in contentpresenter
Thanks in advance

My coding is (using mvvm light)
In View (usercontrol)
XML
<textbox x:name="txtLength" grid.row="13" grid.column="2" borderthickness="2" borderbrush="#B6B6B4" text="{Binding Path=ProductShapeDetails.Length,Mode=TwoWay,ValidatesOnDataErrors=True,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}" margin="0,3,0,2" xmlns:x="#unknown" />

        <contentpresenter name="cpLength" grid.row="14" grid.column="2" content="{Binding ElementName=ProductShapeDetails.Length, Path= (Validation.Errors).CurrentItem}" horizontalalignment="Left">
         <contentpresenter.contenttemplate>
            <datatemplate>
       <textblock foreground="Red" text="{Binding Path= ErrorContent}" />
     </datatemplate>
                                                    </contentpresenter.contenttemplate>
   </contentpresenter>


In view model
C#
public  class RegisterProductViewModel : DataViewModelBase, INotifyPropertyChanged,IDataErrorInfo
    {

      public  static Dictionary<string,>validationErrors = new Dictionary<string,>();

   // in button click

 void ValidateShapes()
        {

            validationErrors.Clear();

             if (SelectedGlaze == EnumGlazeType.None)
                validationErrors.Add("SelectedGlaze","Please select Glaze type");


            if (ProductShapeDetails.Length == 0)

                validationErrors.Add("ProductShapeDetails.Length", "Length is required");


            RaisePropertyChanged(null);
        }

     #region IDataErrorInfo Members
        public string Error
        {
            get
            {
                if (validationErrors.Count > 0)
                {
                    return "Errors found.";
                }
                return null;
            }
        }



        public string this[string columnName]
        {
            get
            {
                if (validationErrors.ContainsKey(columnName))
                {
                    return validationErrors[columnName];
                }
                return null;
            }
        }

        #endregion

}
Posted
Updated 22-May-15 22:34pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900