2010-07-09 4 views
0

또 다른 wpf 질문.wpf 데이터 격자의 바인딩 십진수 값 문제

10 진수 값을 사용하는 두 개의 DataGridTextColumn이 있습니다. 어떤 이유로 새 행을 추가 할 때 (열의 초기 값이 0 임)이 두 열 중 하나에서 값을 두 번 입력해야합니다. 처음 값을 입력하고 탭 아웃하면 값이 0으로 돌아갑니다. 두 번째 값을 입력 한 후에도 그대로 유지됩니다.

<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에지며 EndEdit 이벤트를 추가 한 :

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 열의 기본값이 0이기 때문에 새 행에서 항상 실패합니다. 일단이 수표를 제거하면 문제가 사라졌습니다.

그러나 필수 입력란에 문제가 있습니다. 유효성 검사 프로세스 (IDataError)는 새 행을 입력하는 두 번째 발생시킵니다. 분명히 입력하지 않았고 아직 데이터가 없으므로 대부분의 유효성 검사가 실패하게됩니다. 초기 값을 입력하면 합법적 인 경우에도 값이 지워집니다. 나는 이것이 올바른 행동이라고 생각하지 않고 사용자가 DataGrid 행을 떠날 때까지 초기 유효성 검사를 끄는 방법이 있는지 궁금해하고있었습니다. 그 이외에, 나는 그것이 왜 깨끗하게 될지 확신하지 못한다. 초기 값을 입력 할 때만 발생합니다. 일단 탭 아웃하고 다시 들어가면 값을 입력 할 수 있으며 유효하거나 틀리게 유지됩니다. 올바르지 않은 경우 유효성 검사 스타일은 유효하지 않은 열을 표시합니다.