我在 GridControl 的 Devexpress 中的 DataTemplate 中有一个 CheckBox。绑定到 Grid 的布尔字段。我在自定义列表中添加检查项目(选定的行 ID)。并在 UnChecked 的 Checkbox 上从 Custom List 中删除该项目。最后在单击按钮时,我在单击按钮时插入记录。
问题:当我第一次使用 CheckBox 打开表单时,CheckBox 的 Checked 事件不会触发,但会触发属性更改事件。并单击 INSERT 按钮,它说没有选择任何项目。但是当我选择其他行并单击插入时,它只插入第一个选定的项目,而不是前一个和当前。它错过了电流。为什么会这样?任何想法?
填充.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;
}
}
填充表单.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>
填充表单.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。
帮助赞赏!