Note that there are some explanatory texts on larger screens.

plurals
  1. POValidation using IDataError
    primarykey
    data
    text
    <p>I have the following validation method in my viewmodel (example is showing only one column, "ItemNumber"):</p> <pre><code>public bool IsValid { get { foreach (string property in ValidatedProperties) if (GetValidationError(property) != null) return false; return true; } } static readonly string[] ValidatedProperties = { "ItemNumber" }; string GetValidationError(string propertyName) { if (Array.IndexOf(ValidatedProperties, propertyName) &lt; 0) return null; string error = null; switch (propertyName) { case "ItemNumber": error = this.ValidateItemNumber(); break; default: Debug.Fail("Unexpected property being validated on ProjectExpense: " + propertyName); break; } // set the status message on the UI to the generated error if (error != null) { ErrorMessage = error; } return error; } // string method static bool IsStringMissing(string value) { return String.IsNullOrEmpty(value) || value.Trim() == String.Empty; } string ValidateItemNumber() { if (SelectedProjectExpenseItem != null) { if (IsStringMissing(SelectedProjectExpenseItem.ItemNumber)) { return "Item number is required"; } if (SelectedProjectExpenseItem.ItemNumber.Length &gt; 50) { return "Item number exceeds 50 characters"; } } return null; } #endregion #region IDataErrorInfo Members string IDataErrorInfo.Error { get { return null; } } string IDataErrorInfo.this[string propertyName] { get { return this.GetValidationError(propertyName); } } </code></pre> <p>The validation fires, but I don't know how to communicate it to my datagrid. I am using a separate Dto (hence SelectedProjectExpenseItem.ItemNumber above, SelectedProjectExpenseItem is my Dto) instead of having the properties directly in my viewmodel. Here is my datagrid:</p> <pre><code> &lt;DataGrid ItemsSource="{Binding Path=ListOfProjectExpenseItems}" AutoGenerateColumns="False" Name="dgProjectExpenseItems" SelectionMode="Single" SelectionUnit="FullRow" CanUserResizeColumns="True" SelectedItem="{Binding Path=SelectedProjectExpenseItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" GridLinesVisibility="Horizontal" CanUserDeleteRows="True" CanUserAddRows="True"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="ID" Width="SizeToCells" MinWidth="50" Binding="{Binding RowID}" /&gt; &lt;DataGridTextColumn Header="Project Expense ID" Width="SizeToCells" Visibility="Hidden" MinWidth="0" Binding="{Binding ProjectExpenseID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;DataGridTextColumn Header="Item Number" Width="SizeToCells" MinWidth="140" Binding="{Binding ItemNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;DataGridTextColumn Header="Item Description" Width="SizeToCells" MinWidth="250" Binding="{Binding ItemDescription, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;DataGridTextColumn Header="Unit Price" Width="SizeToCells" MinWidth="90" Binding="{Binding ItemUnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;DataGridTextColumn Header="Qty" Width="SizeToCells" MinWidth="65" Binding="{Binding ItemQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;DataGridTextColumn Header="Supplier Name" Width="SizeToCells" MinWidth="200" Binding="{Binding SupplierName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <p>I would like to use a style, say with a red border and tooltip stating the error, to signify a problem with the row or cell. Every example I found using styles, however, has the properties directly in the viewmodel, and I cannot figure out how to port it over to my Dto.</p> <p>Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload