Skip to main content

Microsoft Silverlight

Unanswered Question CheckBox unchecks inside DataGridTemplateColumnRSS Feed

(0)

scoster32
scoster32

Member

Member

2 points

7 Posts

CheckBox unchecks inside DataGridTemplateColumn

Hello, I have a CheckBox inside a DataGridTemplateColumn.  If I check some of the CheckBox's and then scroll them out of view using the DataGrid scroller they become unchecked.  I understand that using a DataGridCheckBoxColumn does not exhibit this behavior but I need to be able to handle the checked/unchecked events of the CheckBox and from what I understand these events are not available in the DataGridCheckBoxColumn.  Any help would be greatly appreciated.  I have included sample xaml and c# code behind.  Thank you. 

<UserControl x:Class="CheckBoxTest.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:cnt="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White" Height="200">
        <cnt:DataGrid x:Name="dgFiles"  AutoGenerateColumns="False" HeadersVisibility="Column" HorizontalAlignment="Stretch">
            <cnt:DataGrid.Columns>
                <cnt:DataGridTemplateColumn Header="Test CheckBox" Width="40" IsReadOnly="True">
                    <cnt:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <CheckBox VerticalAlignment="Center" HorizontalAlignment="Center" IsThreeState="False" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
                            </Grid>
                        </DataTemplate>
                    </cnt:DataGridTemplateColumn.CellTemplate>
                </cnt:DataGridTemplateColumn>
            </cnt:DataGrid.Columns>
        </cnt:DataGrid>
    </Grid>
</UserControl>
  
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace CheckBoxTest
{
    public partial class Page : UserControl
    {
        public Page()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Page_Loaded);
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            List testList = new List();

            for (int i = 0; i < 25; i++)
            {
                CheckBoxTestData td = new CheckBoxTestData();
                testList.Add(td);
            }

            this.dgFiles.ItemsSource = testList;
        }
    }

    public class CheckBoxTestData
    {
        bool IsSelected { get; set; }
    }
}
 

ken tucker
ken tucker

All-Star

All-Star

16276 points

2,479 Posts

Re: CheckBox unchecks inside DataGridTemplateColumn

 The DataGridTemplateColumn should have a CellTemplate and CellEditingTemplate  when the cell is to be editable.  When the user clicks on a cell it will switch to using the Editing template. What you are doing is placing a editting control in the CellTemplate which is really only meant to display the data.  If you want the same behavior as the CheckBox column I would make the CheckBox in the cell template readonly (IsEnabled=False) and place an editable checkbox in the CellEditingTemplate

 

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn(VS.95).aspx

scoster32
scoster32

Member

Member

2 points

7 Posts

Re: CheckBox unchecks inside DataGridTemplateColumn

Thanks for the reply. I did try what you suggested but it still behaves the same as before.  Whenever I scroll the checkbox's out of view with the datagrid's scroll bar they become unchecked, they even fire their UnChecked events.  I thought it was simply a drawing problem but the fact that the UnChecked event is also raised makes it seem very strange.

shipn
shipn

Member

Member

10 points

8 Posts

Re: CheckBox unchecks inside DataGridTemplateColumn

I am having the exact same problem with the Checkboxes checking/unchecking when I scroll my datagrid.  Any updates in the past 2 months? 

gunjanshah21
gunjansh...

Member

Member

526 points

88 Posts

Re: CheckBox unchecks inside DataGridTemplateColumn

Following Code should work correctly with Silverlight 3:

 
<data:DataGridTemplateColumn Header="CheckBoxColumn">

<data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<CheckBox IsChecked="{Binding Available}" IsEnabled="False"/>

</DataTemplate>

</data:DataGridTemplateColumn.CellTemplate>

<data:DataGridTemplateColumn.CellEditingTemplate>

<DataTemplate>

<CheckBox IsChecked="{Binding Path=Available,Mode=TwoWay}"/>

</DataTemplate>

</data:DataGridTemplateColumn.CellEditingTemplate>

</data:DataGridTemplateColumn>

 I can think of two possible conditions where you might be running into this:

1. Cell Template and CellEditing Template both share the same template.
2. The Binding is not set correctly to TwoWay.
Gunjan Shah [MSFT]

 

Silver22
Silver22

Member

Member

2 points

1 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

I am also facing the same problem. While dragging using the scrollbar, already checked checkboxes are jumbled up and down. any solution??? im using silverlight2. pls help

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

This is not a bug.  What happens when you scroll around in the DataGrid is the same checkboxes are being used for new data because the DataGrid recycles the visuals.  When your new data has different values, the check will change through the Binding and you'll receive the event.  What you can do to get this scenario to work is to listen to LoadingRow which is raised when a row comes into view.  In there, you can call column.GetCellContents to get the contents of the cell.  This will give you the CheckBox, and you can attach to CheckChanged at this time.  If you do this, you need to do something similar and listen to UnloadingRow so you can detach the eventhandler when the checkbox is scrolled out of view.

Yifung Lin [MSFT]

scoster32
scoster32

Member

Member

2 points

7 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Yifung, 

I understand that Silverlight recycles visuals but if I scroll back to a check box shouldn't it go back to it's original state (checked or unchecked)? If this is not bug than it seems like a strange behavior to be by design.  If I understand your post you are telling me that I need to manage the state of checkboxes? This doesn't seem right.

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Yes, it will go back to its original state if it's databound.  The behavior you're seeing where you check the checkboxes, scroll down, and scroll back up and the checkboxes are unchecked are because the DataBinding is OneWay instead of TwoWay.  As a result, when you checked the checkboxes, your item was not updated.  You can see this if you put a breakpoint in the setter of your entity.  If you make it a TwoWay binding, then the changes in the checkbox will be pushed back to your entity, and then when you scroll it back in, it'll be checked.

Yifung Lin [MSFT]

hmaprk
hmaprk

Member

Member

171 points

72 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

I have run into the same situation. It doesn't seem like a good design to force developers to manage the state of that checkbox ourselves. Why force us to keep track of wether we selected a particular record or not by adding a property to the object? All I want to do is list my records and be able to select some of them in order to perform some action on the selected ones. Managing that state is an extra hastle.

 I would still consider this a bug or a very poor design of the datagrid control. Microsoft, please reconsider this feature!!!

Thanks!

hmaprk
http://dotnetgui.blogspot.com/

-Please mark As Answer if this answers your question. Thanks

scoster32
scoster32

Member

Member

2 points

7 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Thank you for your help, this solution worked.  However, I feel that the way Silverlight handles this situation is less than optimum for 2 reasons:

 1.  If you are just using the check box to signify that an item is selected you have to add a property to your entity to manage the state of the check box.

2.  When Silverlight is recycling the visuals, it still fires the Checked, Unchecked events.  Obviously this causes a problem if you are subsribed to these events and are trying to execute code in the eventhandlers.  Unless there is a way to tell if the event is coming from Silverlight or an actual user clicking the checkbox.

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Yes, I do agree that there is a drawback to container recyling.  However, the performance difference between recycling vs not recyling is like light and day.  As a result, container recycling is a clear win for any data sets but the very small ones.

Yifung Lin [MSFT]

Mr. Andersen
Mr. Ande...

Member

Member

2 points

4 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Hi

 we are a new team of silverlight developers that has run into this problem.

Could you please post a smal code example of how to attach/detach to the events on load/unloadRow. And what to do in these events. Note that we cant find the event CheckChanges on the CheckBox control.

 As mentioned earlier we just need to select a number of rows.

 

Hope you can help

nbruckelmyer
nbruckel...

Member

Member

9 points

25 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Do you have an example of this?  I cannot get it working.

kianboo
kianboo

Member

Member

2 points

1 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

Don't know this solution work for you or not, but it work for me.

 Since the grid automatically fire unchecked events of the checkboxes during unloading and loading rows, we just take out checked and unchecked event of the checkbox, replace it by Click event. When user fire Click event, change the IsChecked value to true or false.

 You may also need to attach loading and unloading row event to store the checkboxes state manually. that is, during loadingrow event, loop your stored checkboxe's states and assigning value back to checkbox, and during unloading row event, unchecked the row's checkbox.

 Hope this help :D

sajigeorge
sajigeorge

Member

Member

2 points

1 Posts

Re: Re: CheckBox unchecks inside DataGridTemplateColumn

The same problem i am also having. please share the code for the sample for this issue. it would be greatful.

Thanks and Regards

Saji

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities