2010-07-09 59 views
0

又一個wpf問題。在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綁定到我的虛擬機中的observablecollection。我不知道這有什麼關係呢,但我已經通過創建一個單獨的類(用於保存數據的用戶離開行時)增加了一個EndEdit中的事件對我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 
} 

}

回答

0

這不是技術上的答案,但我發現這個問題是以下幾點:

 if (propertyName == "ItemQty") 
     { 
      if (this.ItemQty == 0.0M) 
       result = "Item quantity cannot be empty!"; 
     } 

因爲dg列的默認值爲零,所以這會在新行上始終失敗。一旦我擺脫了這個檢查,問題就消失了。

但是,我也有一個必填字段的問題。驗證過程(IDataError)觸發第二個輸入新行。顯然,大多數驗證都會失敗,因爲您還沒有輸入數據。當你輸入一個初始值時,即使它是合法的,該值也會被清除。我不認爲這是正確的行爲,並想知道是否有方法關閉初始驗證,直到用戶離開數據網格行。除此之外,我不知道爲什麼它會被清除。只有在輸入初始值時纔會發生。一旦你退出並返回,你可以輸入一個值,它會保持它,有效或沒有。如果無效,則驗證樣式應該按照標記無效列。