为什么CheckEdit事件在WPF中的GridControl中首次尝试选中项时不会触发? [英] Why CheckEdit event doesn't fire on first attempt of checked item in GridControl in WPF?

查看:211
本文介绍了为什么CheckEdit事件在WPF中的GridControl中首次尝试选中项时不会触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在GridControl的Devexpress中的DataTemplate中有一个复选框。其中绑定到Grid的boolean字段。我在自定义列表中添加选中的项目(选定的行ID)。并在未选中复选框从自定义列表中删除项目。



问题:当我打开表单第一次使用CheckBox选择一个项目CheckBox事件的CheckBox不发火,但属性改变事件触发。并点击INSERT按钮表示没有选择项目。但是当我选择其他行并且点击插入它只插入第一个选定项目,而不是前一个和当前。它错过了当前。为什么会发生这种情况?任何想法?



Infill.cs

  public partial class Infill:INotifyPropertyChanged 
{
public int InfillID {get;组; }
//这里的其他字段
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字段,T值,字符串属性名)
{
if(EqualityComparer T .Default.Equals(field,value) )return false;
field = value;
OnPropertyChanged(propertyName);
return true;
}
}

InfillForm.xaml / p>

 < dxg:GridControl Height =500Name =grdInfillVerticalAlignment =Center> 
< dxg:GridControl.Columns>
< dxg:GridColumn AllowEditing =TrueWidth =10>
< dxg:GridColumn.CellTemplate>
< DataTemplate>
< CheckBox Name =chkSelectVisibility =HiddenHorizo​​ntalAlignment =CenterIsChecked ={Binding Path = RowData.Row.IsChecked,Mode = TwoWay}Checked =CheckEdit_CheckedUnchecked =CheckEdit_Unchecked />
< / DataTemplate>
< / dxg:GridColumn.CellTemplate>
< / dxg:GridColumn>
< dxg:GridColumn FieldName =IsCheckedHeader =Select/>
< dxg:GridControl.View>
< dxg:TableView Name =grdInfillInnerShowTotalSummary =TrueAutoWidth =TrueDetailHeaderContent =TrueShowIndicator =FalseShowGroupPanel =FalseCellValueChanging =grdInfillInner_CellValueChanging>
< / dxg:TableView>
< / dxg:GridControl.View>
< / dxg:GridControl>

InfillForm.xaml.cs

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

}

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示例添加或删除项目到列表,基于CHECKED或UNCHECKED!
int infillid = item.InfillID;
infillListIDs.Add(infillid);
result = true;
}
}
else
{
if(item!= null)
{
if(infillListIDs.Contains(item.InfillID) )
{
//如果解开选中的项目,然后从自定义列表中删除
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)
{
//删除现有并插入新项目在表中每次点击
BLL.DeleteAllInfillPO();
if(infillListIDs.Count> 0)
{

for(int i = 0; i< infillListIDs.Count; i ++)
{
// insert selected数据库中检查的项目ID
BLL.GetInfillIDAndInsertIntoInfillPO(infillListIDs [i]);
}

BtnView.Visibility = Visibility.Visible;
BtnInsert.Visibility = Visibility.Hidden;
//显示插入项的报告
OpenWindow();
}
else
{
MessageBox.Show(请从列表中选择项目,选择选项,MessageBoxButton.OK,MessageBoxImage.Exclamation);
}
}

我无法通过名称获得复选框是在DataTemplate内部,因此添加了布尔字段并绑定到内部datatemplate中的CheckBox。



帮助欣赏!

解决方案

CellTemplate 应该有一个名为PART_Editor / p>

更改此

 <复选框名称=chkSelect ./> 

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

并且在WPF中编程时请使用MVVM。类似于winforms的代码后面的类型的东西是令人厌恶的。


I have a CheckBox in DataTemplate in Devexpress of GridControl. Which is binded to boolean field of Grid. I am adding the Checked Item (selected Row ID) in Custom List. And on UnChecked of Checkbox removing the item from Custom List . and finally on button click i am inserting the records on button click.

Issue: When i open form first time select an item using CheckBox the Checked event of CheckBox doesn't fire but property change event fires. and on clicking INSERT button it says no item selected. but when i select other row and Click on Insert it Inserts the First Selected item only and not the both previous and Current. It misses the current. Why does this happen? Any Idea?

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);
            }
        }

I am not able to get the Checkbox by name that is inside DataTemplate so added boolean field and binded to CheckBox that is inside datatemplate.

Help Appreciated!

解决方案

The CellTemplate should have an element named "PART_Editor".

change this

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

for this:

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

and please use MVVM when programming in WPF. winforms-like code behind type of stuff is disgusting.

这篇关于为什么CheckEdit事件在WPF中的GridControl中首次尝试选中项时不会触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆