2013-05-30 28 views
0

我在GridControl的Devexpress的DataTemplate中有一個CheckBox。其綁定到網格的布爾字段。我在自定義列表中添加選中的項目(選定的行ID)。並在UnChecked of Checkbox從自定義列表中刪除項目。最後按鈕點擊我插入按鈕點擊記錄。爲什麼CheckEdit事件不會在WPF中的GridControl中檢查項目的第一次嘗試時觸發?

問題:當我打開表格第一次使用CheckBox選擇一個項目CheckBox Checked事件不會觸發,但屬性更改事件觸發。並在點擊INSERT按鈕時表示沒有選擇項目。但是當我選擇其他行並單擊插入它只插入第一個選定的項目,而不是先前和當前。它錯過了當前。這是爲什麼發生?任何想法?

Infill.cs

public partial class Infill:INotifyPropertyChanged 
    { 
     public int InfillID { get; set; } 
    //some other fields here 
    private bool isChecked; 
     public bool IsChecked { get { return isChecked; } set { SetField(ref isChecked, value, "IsChecked"); } } 
     public event PropertyChangedEventHandler PropertyChanged; 

     protected virtual void OnPropertyChanged(string propertyName) 
     { 
      PropertyChangedEventHandler handler = PropertyChanged; 
      if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
     protected bool SetField<T>(ref T field, T value, string propertyName) 
     { 
      if (EqualityComparer<T>.Default.Equals(field, value)) return false; 
      field = value; 
      OnPropertyChanged(propertyName); 
      return true; 
     } 
    } 

InfillForm.xaml

<dxg:GridControl Height="500" Name="grdInfill" VerticalAlignment="Center"> 
      <dxg:GridControl.Columns> 
         <dxg:GridColumn AllowEditing="True" Width="10"> 
            <dxg:GridColumn.CellTemplate> 
             <DataTemplate>  
               <CheckBox Name="chkSelect" Visibility="Hidden" HorizontalAlignment="Center" IsChecked="{Binding Path=RowData.Row.IsChecked, Mode=TwoWay}" Checked="CheckEdit_Checked" Unchecked="CheckEdit_Unchecked"/> 
              </DataTemplate> 
           </dxg:GridColumn.CellTemplate> 
         </dxg:GridColumn> 
      <dxg:GridColumn FieldName="IsChecked" Header="Select" /> 
<dxg:GridControl.View> 
<dxg:TableView Name="grdInfillInner" ShowTotalSummary="True" AutoWidth="True" DetailHeaderContent="True" ShowIndicator="False" ShowGroupPanel="False" CellValueChanging="grdInfillInner_CellValueChanging"> 
    </dxg:TableView> 
     </dxg:GridControl.View> 
</dxg:GridControl> 

InfillForm.xaml.cs

private void CheckEdit_Checked(object sender, RoutedEventArgs e) 
     { 
      e.Handled = ProcessItem(true); 

     } 

     private void CheckEdit_Unchecked(object sender, RoutedEventArgs e) 
     { 
      e.Handled = ProcessItem(false); 

     } 
    private bool ProcessItem(bool IsChecked) 
     { 
      bool result = false; 
      Infill item = grdInfillInner.FocusedRow as Infill; 
      if (IsChecked) 
      { 
       if (item != null) 
       { 
        // DO STUFF HERE EXAMPLE ADD or REMOVE Item to a list, BASED on CHECKED or UNCHECKED!!! 
        int infillid = item.InfillID; 
        infillListIDs.Add(infillid); 
        result = true; 
       } 
      } 
      else 
      { 
       if (item != null) 
       { 
        if (infillListIDs.Contains(item.InfillID)) 
        { 
         // if uncheked the checked item then remove from custom list 
         infillListIDs.Remove(item.InfillID); 
        } 
       } 
      } 
      grdInfillInner.FocusedRowHandle = -1; 
      return result; 
     } 
protected void OpenWindow() 
     { 
      ReportPopup popup = new ReportPopup(); 
      popup.Owner = this; 
      popup.WindowStartupLocation = WindowStartupLocation.CenterScreen; 
      popup.ShowDialog(); 

     } 

private void MnuBtnInsert_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e) 
     { 
      //Delete existing and insert new items in table on every click 
      BLL.DeleteAllInfillPO(); 
      if (infillListIDs.Count > 0) 
      { 

       for (int i = 0; i < infillListIDs.Count; i++) 
       { 
        //insert selected Checked items id in database 
        BLL.GetInfillIDAndInsertIntoInfillPO(infillListIDs[i]); 
       } 

       BtnView.Visibility = Visibility.Visible; 
       BtnInsert.Visibility = Visibility.Hidden; 
       //show report of inserted items 
       OpenWindow(); 
      } 
      else 
      { 
       MessageBox.Show("Please select item/s from list", "Select Option", MessageBoxButton.OK, MessageBoxImage.Exclamation); 
      } 
     } 

我不是ABL e通過DataTemplate中的名稱獲取複選框,以便添加布爾字段並綁定到位於datatemplate內部的CheckBox。

幫助讚賞!

回答

1

CellTemplate應該有一個名爲「PART_Editor」的元素。

改變這種

<CheckBox Name="chkSelect" .../> 

此:

<CheckBox x:Name="PART_Editor" .../> 

,並請在WPF編程時使用MVVM。背後的類似winforms的代碼令人厭惡。

+0

如果你不介意。請您提供一些示例代碼來解決上述問題。我是WPF的新手。或點擊按鈕單擊時訪問選中項目的方法? –

0

<grid:GridColumn AllowEditing="True" Header="Completed"> <grid:GridColumn.CellTemplate> <DataTemplate> <CheckBox x:Name="PART_Editor" IsChecked="{Binding Path=Data.CompletedField}" HorizontalAlignment="Center" VerticalAlignment="Center"/> </DataTemplate> </grid:GridColumn.CellTemplate> </grid:GridColumn>

記得添加Data.Field在綁定路徑。祝你好運。

相關問題