Welcome to the Xceed Community | Help
Community Search  
More Search Options

WPF Grid CheckBox behavior

Sort Posts: Previous Next
  •  03-05-2009, 8:09 AM Post no. 19073

    WPF Grid CheckBox behavior

    Hi !

     I have strange behavior of CheckBox columns of WPF Grid 3.1 (the latest version fron 17 February 2009).

     There is small application, with TabControl of two tabs. The first tab control contains Xceed WPF grid control. The first column bind to some boolean variable. The problem occures then I check the CheckBox, than do not move to any other place in grid, just click on other tab item. When I return to the previous tab I see CheckBox still checked, but my variable restored to it's previous state. But if I'll move to another row of the grid after checking the checkbox everithing works fine.

     My grid defined with UpdateSourceTrigger="CellContentChanged".

    Please, help me to solve this problem.

    Thanks!

    Igor.

     

     

  •  04-21-2009, 5:23 AM Post no. 20410 in reply to 19073

    Re: WPF Grid CheckBox behavior

    Hi,

    I have the same problem.

    On CheckBox cells, I have to exit the cell in ordre for changes to be applied.

    I also use the CellContentChanged property for UpdateSourceTrigger but it doesn't change anything.

     

    Does anyone know how to do that ? 

    Thanks. 

  •  04-21-2009, 7:07 AM Post no. 20415 in reply to 20410

    Re: WPF Grid CheckBox behavior

    Xceed I also have the same issue, 3 guys with the same issue on the same day nearly :)

     

    Lets hope there is a quick a simple solution.

  •  04-21-2009, 7:20 AM Post no. 20416 in reply to 20415

    Re: WPF Grid CheckBox behavior

    My problem seems to come from the fact that my data source is a DataTable that I create manually in code-behind. 

    Still searching a solution... 

  •  04-21-2009, 9:07 AM Post no. 20422 in reply to 20416

    Re: WPF Grid CheckBox behavior

    Some improvement.

    I managed to force my data to update by catching the EditBeginning EditBegun and EditEnded events and by doing this when they occur : 

    [CODE]

            public void CellEditEnded(object sender, RoutedEventArgs e)

            {

                DataCell cell = (DataCell)sender;

                if (cell.ParentColumn.Index == 2) //the Boolean column

                {

                    // Update my data manually

                }

            }

     

            public void CellEditBeginning(object sender, CancelRoutedEventArgs e)

            {

                DataCell cell = (DataCell)sender;

                if (cell.ParentColumn.Index == 2)

                {

                    // Updates the cell's content immediately.

                    cell.Content = !(bool)cell.Content;

                }

            }

            

            public void CellEditBegun(object sender, RoutedEventArgs e)

            {

               DataCell cell = (DataCell)sender;

                if (cell.ParentColumn.Index == 2)

                {

                    // End the edit action in order to commit the changes directly on the click.

                    cell.EndEdit();

                }

            }

    [/CODE] 

    This way, my data are automatically updated when I click on the cell.

    The only remaining problem is that when I click on another tab and that I come back to the previous one, if I click on the selected boolean cell, I do not enter in any of the previous events.

    Still investigating... 

  •  04-21-2009, 9:49 AM Post no. 20423 in reply to 20422

    Re: WPF Grid CheckBox behavior

    In fact, I have another problem when switching from one tab to another.

    If I do this several times in a row, the status of the last checkbox I clicked changes each time I come back on the corresponding tab.

    I can't figure out how to commit my data changes easily...

  •  04-21-2009, 10:03 AM Post no. 20425 in reply to 20423

    Re: WPF Grid CheckBox behavior

    I'm sure there's got to be a better solution in the xceed grid, this would be rather poor if you had to resort to this :(
  •  04-21-2009, 11:53 AM Post no. 20432 in reply to 20425

    Re: WPF Grid CheckBox behavior

    Sorry, I didn't see this thread. I created another one of the sort of problem. It's not only a problem with checkboxes. Other editable columns show the same symptoms. The pending changes are getting lost when the grid is unloaded (goes invisible or its container like Tab becomes inactive). Which is not the default behavior of standalone standard widgets when they are collapsed.

  •  04-22-2009, 8:37 AM Post no. 20457 in reply to 20432

    Re: WPF Grid CheckBox behavior

    Guys

    The only way I could get this working is by adding an event handler to the Checked / Unchecked events shown below

            /// <summary>
            /// Called when the state of the selected check box within a row in the grid changes to
            /// force the edit to finish in the cell.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void SelectedCheckBox_CheckedStateChanged(object sender, RoutedEventArgs e)
            {
                if (PaymentGrid.CurrentItem == null)
                {
                    return;
                }

                Xceed.Wpf.DataGrid.DataRow row = PaymentGrid.GetContainerFromItem(PaymentGrid.CurrentItem) as Xceed.Wpf.DataGrid.DataRow;
                if (row != null)
                {
                    row.Cells["Selected"].EndEdit();
                }
            }

    My XAML for the checkbox is defined below

                    <xcdg:Column FieldName="Selected" Width="30" CellEditorDisplayConditions="Always" >
                        <xcdg:Column.CellEditor>
                            <xcdg:CellEditor>
                                <xcdg:CellEditor.EditTemplate>
                                    <DataTemplate>
                                        <Grid Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                            <CheckBox x:Name="SelectedCheckBox" Margin="2,2,2,2"  HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{xcdg:CellEditorBinding}" Checked="SelectedCheckBox_CheckedStateChanged" Unchecked="SelectedCheckBox_CheckedStateChanged"/>
                                        </Grid>
                                    </DataTemplate>
                                </xcdg:CellEditor.EditTemplate>
                            </xcdg:CellEditor>
                        </xcdg:Column.CellEditor>
                        <xcdg:Column.CellContentTemplate>
                            <DataTemplate>
                                <Grid Width="Auto" Height="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                    <CheckBox Margin="2,2,2,2"  HorizontalAlignment="Center" VerticalAlignment="Center" IsChecked="{xcdg:CellEditorBinding}"/>
                                </Grid>
                            </DataTemplate>
                        </xcdg:Column.CellContentTemplate>
                        <xcdg:Column.TitleTemplate>
                            <DataTemplate>
                                <CheckBox Name="SelectedCheckBox" HorizontalAlignment="Center" ToolTip="Select All" VerticalAlignment="Center" Loaded="SelectedCheckBox_Loaded" />
                            </DataTemplate>
                        </xcdg:Column.TitleTemplate>
                    </xcdg:Column>
     

    I'd like to know from Xceed why this has to be done and you can't simply use IsChecked="{xcdg:CellEditorBinding}" and ensure the UpdateSourceTrigger is set to CellContentChanged.

     

  •  04-22-2009, 8:51 AM Post no. 20458 in reply to 20457

    Re: WPF Grid CheckBox behavior

    I found a solution in order to validate the changes on some events.

    The key is just to end the edit mode of the DataGrid when you change of TabItem (or when you click on a button that has to retrieve the correct data, etc). You just have to catch the TabControl.SelectionChanged event (or whatever event that suits your needs) and call the following method : 

    [CODE] 
                if (myDataGrid.IsBeingEdited)
                {
                    myDataGrid.EndEdit();
                }
    [/CODE] 

    It seems to solve the problem... 
     
    But that still does not update my boolean values each time I click on a CheckBox.
  •  05-13-2009, 8:32 AM Post no. 20898 in reply to 20458

    Re: WPF Grid CheckBox behavior

    I have this problem too.
  •  05-14-2009, 4:09 AM Post no. 20929 in reply to 20898

    Re: WPF Grid CheckBox behavior

    Is there a solution or a workaround to this? I have a grid bound to an observable collection but the user can swap which observablecollection is being bound so the last edits are lost when the user swaps between collections. I can't tell them to press enter when they are done editing because they will forget.

     I have tried the workarounds mentioned earlier in this thread but they don't seem to work - if anything they just prevent any further editing.

  •  08-31-2009, 4:17 PM Post no. 23613 in reply to 20929

    Re: WPF Grid CheckBox behavior

    Hi all,

    I was running into this same problem and not able to find a satisfactory solution if you define your own editor template.  Xceed does provide a default editor, though, if you specify the DataType as Boolean on the DataGridItemProperty in the CollectionViewSource, and this seems to update the source just fine.  If you need additional control over the editor, this will not work for you, but if all you need is a working checkbox bound to a property, then this should be fine.

     Here is the XAML.

    In Resources:

    <xcdg:DataGridCollectionViewSource x:Key="cvsData"
                                               Source="{Binding Path=Data}"
                                               AutoCreateDetailDescriptions="False"
                                               AutoCreateItemProperties="True" >

            <xcdg:DataGridCollectionViewSource.ItemProperties>
                    <!--Define this cell manually so we can use the default editor for the boolean type. (checkbox)-->
                    <xcdg:DataGridItemProperty
                        Name="Selected"
                        Title=""
                        DataType="{x:Type System:Boolean}"
                        />
            </xcdg:DataGridCollectionViewSource.ItemProperties>

    </xcdg:DataGridCollectionViewSource>

     And in the main body:

     <xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvsData}}">

            <xcdg:DataGridControl.Columns>
                    <xcdg:Column FieldName="Selected" Title="" Width="24" ReadOnly="False" CellEditorDisplayConditions="MouseOverCell,RowIsCurrent,CellIsCurrent"/>

            </xcdg:DataGridControl.Columns>

    </xcdg:DataGridControl>

     Hope this helps!  I'm still wondering how to get a user-defined DataTemplate to work with a checkbox, though...

View as RSS news feed in XML
Contact | Site Map | Reviews | Legal Terms of Use | Trademarks | Privacy Statement Copyright 2011 Xceed Software Inc.