又一个wpf问题。
我有两个采用十进制值的DataGridTextColumn。出于某种原因,当我添加一个新行(列的初始值为零)时,我必须在这两列中的任何一列中输入两次值。我第一次在其中输入一个值并标签,该值返回零。在我第二次输入值之后。
<DataGridTextColumn Header="Unit Price" EditingElementStyle="{StaticResource CellEditStyle}" Width="SizeToCells" MinWidth="90" Binding="{Binding ItemUnitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
<DataGridTextColumn Header="Qty" EditingElementStyle="{StaticResource CellEditStyle}" Width="SizeToCells" MinWidth="65" Binding="{Binding ItemQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" />
dg与我的vm中的可观察集合绑定。我不确定它与它有什么关系,但是我通过创建一个单独的类(用于在用户离开行时保存数据)向我的OC添加了一个endit事件:
public class ObservableProjectExpenseItems : ObservableCollection<ProjectExpenseItemsBO>
{
protected override void InsertItem(int index, ProjectExpenseItemsBO item)
{
base.InsertItem(index, item);
item.ItemEndEdit += new ProjectExpenseItemsBO.ItemEndEditEventHandler((x) =>
{
if (ItemEndEdit != null)
ItemEndEdit(x);
});
}
public event ProjectExpenseItemsBO.ItemEndEditEventHandler ItemEndEdit;
}
我的业务对象如下所示:
public class ProjectExpenseItemsBO : IDataErrorInfo, IEditableObject
{
public int RowID { get; set; }
public int ProjectExpenseID { get; set; }
public string ItemNumber { get; set; }
public string ItemDescription { get; set; }
public decimal ItemUnitPrice { get; set; }
public decimal ItemQty { get; set; }
public string SupplierName { get; set; }
public DateTime CreateDate { get; set; }
public ProjectExpenseItemsBO()
{
}
// string method
static bool IsStringMissing(string value)
{
return String.IsNullOrEmpty(value) || value.Trim() == String.Empty;
}
private bool _isValid = true;
public bool IsValid
{
get { return _isValid; }
set { _isValid = value; }
}
#region IDataErrorInfo Members
public string Error
{
get
{
return this[string.Empty];
}
}
public string this[string propertyName]
{
get
{
string result = string.Empty;
if (propertyName == "ProjectExpenseID")
{
if (this.ProjectExpenseID == 0)
result = "An existing project expense item must be selected!";
}
if (propertyName == "ItemNumber")
{
if (this.ItemNumber != null)
{
if (IsStringMissing(this.ItemNumber))
result = "Item number cannot be empty!";
if (this.ItemNumber.Length > 50)
result = "Item number cannot be longer than 50 characters!";
}
}
if (propertyName == "ItemDescription")
{
if (this.ItemDescription != null)
{
if (this.ItemDescription.Length > 256)
result = "Item description cannot be longer than 256 characters!";
}
}
if (propertyName == "ItemUnitPrice")
{
if (this.ItemUnitPrice == 0.0M)
result = "Item unit price cannot be empty!";
}
if (propertyName == "ItemQty")
{
if (this.ItemQty == 0.0M)
result = "Item quantity cannot be empty!";
}
if (propertyName == "SupplierName")
{
if (this.SupplierName != null)
{
if (this.SupplierName.Length > 128)
result = "Item number cannot be longer than 128 characters!";
}
}
if (result.Length > 0)
IsValid = false;
else
IsValid = true;
return result;
}
}
#endregion
#region IEditableObject Members
public delegate void ItemEndEditEventHandler(IEditableObject sender);
public event ItemEndEditEventHandler ItemEndEdit;
public void BeginEdit()
{
//throw new NotImplementedException();
}
public void CancelEdit()
{
//throw new NotImplementedException();
}
public void EndEdit()
{
if (ItemEndEdit != null)
{
ItemEndEdit(this);
}
}
#endregion
}
}
答案 0 :(得分:0)
这在技术上不是答案,但我发现问题如下:
if (propertyName == "ItemQty")
{
if (this.ItemQty == 0.0M)
result = "Item quantity cannot be empty!";
}
这在新行上总是会失败,因为dg列的默认值为零。一旦我摆脱了这个检查,问题就消失了。
但是,我也遇到了必填字段的问题。验证过程(IDataError)会触发您输入新行的第二行。显然,大多数验证都会因为您尚未输入数据而失败。当您输入初始值时,即使它是合法的,该值也会被清除。我不认为这是正确的行为,并想知道是否有办法关闭初始验证,直到用户离开datagrid行。除此之外,我不确定为什么会被清除。它只在输入初始值时发生。一旦您退出并重新登录,您就可以输入一个值,它会保留,有效与否。如果无效,验证样式将标记无效列。