wpf datagrid中绑定十进制值的问题
还有一个wpf问题 我有两个DataGridTextColumn,它们接受十进制值。出于某种原因,当我添加新行(列的初始值为零)时,我必须在这两列中的任何一列中输入两次值。我第一次在其中键入值并进行制表时,该值返回到零。在我第二次输入值后,它将保持不变wpf datagrid中绑定十进制值的问题,wpf,datagrid,binding,Wpf,Datagrid,Binding,还有一个wpf问题 我有两个DataGridTextColumn,它们接受十进制值。出于某种原因,当我添加新行(列的初始值为零)时,我必须在这两列中的任何一列中输入两次值。我第一次在其中键入值并进行制表时,该值返回到零。在我第二次输入值后,它将保持不变 <DataGridTextColumn Header="Unit Price" EditingElementStyle="{StaticResource CellEditStyle}" Width="SizeToCells" MinWidt
<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}" />
}从技术上讲,这不是一个答案,但我发现问题如下:
if (propertyName == "ItemQty")
{
if (this.ItemQty == 0.0M)
result = "Item quantity cannot be empty!";
}
由于dg列的默认值为零,因此这在新行上总是失败的。一旦我摆脱了这张支票,问题就消失了
但是,我也有一个必填字段的问题。输入新行后,验证过程(IDataError)将立即启动。显然,大多数验证都会失败,因为您还没有输入数据。但是,当您输入初始值时,即使它是合法的,该值也会被清除。我认为这不是正确的行为,我想知道是否有办法在用户离开datagrid行之前关闭初始验证。除此之外,我不知道为什么它会被清除。它仅在输入初始值时发生。一旦你签出并返回,你可以输入一个值,它将保持它,有效或无效。如果无效,验证样式将按其应有的方式标记无效列
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
}
if (propertyName == "ItemQty")
{
if (this.ItemQty == 0.0M)
result = "Item quantity cannot be empty!";
}