gpt4 book ai didi

wpf - 使用 IDataError 进行验证

转载 作者:行者123 更新时间:2023-12-03 10:51:07 26 4
gpt4 key购买 nike

我的 View 模型中有以下验证方法(示例仅显示一列“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),而不是直接在我的 View 模型中使用属性。这是我的数据网格:
  <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>

我想使用一种样式,比如带有红色边框和说明错误的工具提示,来表示行或单元格有问题。然而,我发现的每个使用样式的示例都直接在 View 模型中具有属性,我无法弄清楚如何将其移植到我的 Dto 中。

任何帮助表示赞赏。

最佳答案

你有ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=true活跃在你的绑定(bind)上?

关于wpf - 使用 IDataError 进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3112811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com