为什么在WPF中的GridControl中首次尝试检查项时不会触发CheckEdit事件?

时间:2013-05-30 14:33:14

标签: c# wpf checkbox devexpress

我在GridControl的Devexpress中的DataTemplate中有一个CheckBox。哪个绑定到Grid的布尔字段。我在自定义列表中添加了Checked Item(选定的行ID)。在UnChecked的Checkbox中,从自定义列表中删除该项目。最后点击按钮我在点击按钮时插入记录。

问题:当我第一次打开表单使用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);
            }
        }

我无法通过DataTemplate内部的名称获取Checkbox,因此添加了boolean字段并绑定到datatemplate内的CheckBox。

帮助感谢!

2 个答案:

答案 0 :(得分:1)

CellTemplate应该有一个名为“PART_Editor”的元素。

更改此

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

为此:

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

并且在WPF中编程时请使用MVVM。类似winforms的代码背后的东西很恶心。

答案 1 :(得分: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。祝你好运。