我的viewmodel中有以下验证方法(示例只显示一列,“ItemNumber”):
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) < 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 > 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); }
}
验证触发,但我不知道如何将其传递给我的数据网格。我使用单独的Dto(因此上面的SelectedProjectExpenseItem.ItemNumber,SelectedProjectExpenseItem是我的Dto)而不是直接在我的viewmodel中具有属性。这是我的datagrid:
<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">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Width="SizeToCells" MinWidth="50" Binding="{Binding RowID}" />
<DataGridTextColumn Header="Project Expense ID" Width="SizeToCells" Visibility="Hidden" MinWidth="0" Binding="{Binding ProjectExpenseID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Item Number" Width="SizeToCells" MinWidth="140" Binding="{Binding ItemNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Item Description" Width="SizeToCells" MinWidth="250" Binding="{Binding ItemDescription, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Unit Price" Width="SizeToCells" MinWidth="90" Binding="{Binding ItemUnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Qty" Width="SizeToCells" MinWidth="65" Binding="{Binding ItemQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<DataGridTextColumn Header="Supplier Name" Width="SizeToCells" MinWidth="200" Binding="{Binding SupplierName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
</DataGrid>
我想使用样式,例如带有红色边框和工具提示,说明错误,表示行或单元格的问题。然而,我发现使用样式的每个例子都直接在viewmodel中有属性,我无法弄清楚如何将它移植到我的Dto。
感谢任何帮助。
答案 0 :(得分:0)
你的绑定有ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=true
个活动吗?
答案 1 :(得分:0)
问题与在我的实现中使用Dto有关,或者至少在其中没有实现IDataErrorInfo。我决定抛弃它们并使用实现IDataErroInfo和wa-la的直接业务对象,现在一切都很美妙。