Reputation: 6056
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!
Upvotes: 0
Views: 1866
Reputation: 581
<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>
Remember to add the Data.Field in the Binding Path. Good luck.
Upvotes: 0
Reputation: 44038
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.
Upvotes: 1