Еще один вопрос 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}" />
ДГ связан с наблюдаемой коллекцией в моей виртуальной машине. Я не уверен, что это как-то связано с этим, но я добавил событие endit в свой OC, создав отдельный класс (используется для сохранения данных, когда пользователи покидают строку):
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
}
}