Skip to main content

Microsoft Silverlight

Unanswered Question Checkbox event in datagrid cannot be detected..RSS Feed

(0)

slen
slen

Member

Member

10 points

59 Posts

Checkbox event in datagrid cannot be detected..

 <my:DataGridTemplateColumn>
                        <my:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox x:Name="rowckbx" Tag="{Binding id}" Checked="rowckbx_Checked" Unchecked="rowckbx_Unchecked"   />
                            </DataTemplate>
                        </my:DataGridTemplateColumn.CellTemplate>
 </my:DataGridTemplateColumn>

 My question is why rowckbx_Checked and rowckbx_Unchecked cannot be called? In the event handler function,  I put something like

  Browser.HtmlPage.Window.Alert("::")

To detect whether this  event is called....

Are there things I missed?

Thanks

varshavmane
varshavmane

Contributor

Contributor

6721 points

1,579 Posts

Re: Checkbox event in datagrid cannot be detected..

Hi,

It works fine with me. Here is the code which that I used:

<Data:DataGrid x:Name="dataGridCategories" Margin="20" AutoGenerateColumns="False"

MaxHeight="250" MaxWidth="385" VerticalScrollBarVisibility="Auto"

HorizontalScrollBarVisibility="Disabled" HeadersVisibility="Column"

HorizontalGridLinesBrush="{x:Null}" Foreground="{x:Null}" Width="385">

<Data:DataGrid.Columns>

<Data:DataGridTemplateColumn Visibility="Visible" Header="Category Name" Width="120">

<Data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<HyperlinkButton Content="{Binding Path=CategoryName}" Click="HyperlinkButton_Click" TargetName="{Binding Path=CategoryId}" />

</DataTemplate>

</Data:DataGridTemplateColumn.CellTemplate>

</Data:DataGridTemplateColumn>

<Data:DataGridTextColumn Header="CategoryID" Width="120"

Binding="{Binding CategoryId}"/>

<Data:DataGridTemplateColumn Header="Delete">

<Data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<HyperlinkButton x:Name="deleteButton" Content="Delete" IsTabStop="True" Tag="{Binding CategoryId}" Click="deleteButton_Click"></HyperlinkButton>

</DataTemplate>

</Data:DataGridTemplateColumn.CellTemplate>

</Data:DataGridTemplateColumn>

<Data:DataGridTemplateColumn Header="Checkbox">

<Data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<CheckBox x:Name="rowckbx" Tag="{Binding CategoryId}" Checked="rowckbx_Checked" Unchecked="rowckbx_Unchecked" />

</DataTemplate>

</Data:DataGridTemplateColumn.CellTemplate>

</Data:DataGridTemplateColumn>

</Data:DataGrid.Columns>

</Data:DataGrid>

Code Behind:

for (int i = 0; i < 10; i++)categories.Add(new Categories("test_" + i, i));

dataGridCategories.ItemsSource = categories;

private void rowckbx_Checked(object sender, RoutedEventArgs e)

{

System.Windows.Browser.
HtmlPage.Window.Alert("::");

}

private void rowckbx_Unchecked(object sender, RoutedEventArgs e)

{

System.Windows.Browser.
HtmlPage.Window.Alert("::");

}

I guess the problem might be with Itemssource. Please check that.

HTH Smile

Please "Mark as Answer" if this post answered your question. :)
Visit my Blog: http://varshavmane.blogspot.com/

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Checkbox event in datagrid cannot be detected..

 try to use break point to see if you enter in your events.

and after try with Tag="". It's may be an issue with your binding.

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Checkbox event in datagrid cannot be detected..

I tried with tag="". It doesn't work. The event was not called.  

What do you mean by break point? How do I enter break point in silverlight of the function?

 

Thanks 

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Checkbox event in datagrid cannot be detected..

My Itemsource seems good to me. The source is assigned to the datagrid once the page get loaded.

Thanks


 

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Checkbox event in datagrid cannot be detected..

My Itemsource seems good to me. The source is assigned to the datagrid once the page get loaded.

Thanks


 

varshavmane
varshavmane

Contributor

Contributor

6721 points

1,579 Posts

Re: Re: Re: Checkbox event in datagrid cannot be detected..

Did you check with the code that I have posted ?

Please "Mark as Answer" if this post answered your question. :)
Visit my Blog: http://varshavmane.blogspot.com/

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 Yes. It is almost the same as mine. The thing that is different is that my itemsource is assigned thought a web service called.

Thanks

varshavmane
varshavmane

Contributor

Contributor

6721 points

1,579 Posts

Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

That doesnt make any difference.

Can you post your WCF code ?

Please "Mark as Answer" if this post answered your question. :)
Visit my Blog: http://varshavmane.blogspot.com/

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

The following code is the part from WCF.

Private Sub Page_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)

webService = New ServiceReference2.ServiceCountryClient
AddHandler webService.GetCountryCompleted, AddressOf webService_GetCountryCompleted

webService.GetCountryAsync()

 End Sub

Private Sub webService_GetCountryCompleted(ByVal sender As Object, ByVal e As CountrySilverlight.ServiceReference2.GetCountryCompletedEventArgs)

        source = e.Result
        theDataGrid.ItemsSource = source ' source actually is an ObservableCollection object

    End Sub

 

Thanks

varshavmane
varshavmane

Contributor

Contributor

6721 points

1,579 Posts

Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

What you get in e.Result ?

Please "Mark as Answer" if this post answered your question. :)
Visit my Blog: http://varshavmane.blogspot.com/

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

Is the collection of data. I assigned to source because I want to store that object. It returns ObservableCollection object ...

Thannks

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 you can add :

myDatagrid.ItemsSource = null before the myDatagrid.ItemsSource = myCollection...

what's the type of your "Id" property ??

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 The Id is in integer type ...

I tried it setting it to null first .. but it didn't work ..

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 I'ts better for a check box to bind with a boolean.....

you can use an IValueConverter to return the good value ....

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

If I bind it with boolean, it only gives me true/false value. The reason I put an id there is because when determine the checkbox is checked, I can retrieve the id to search in the database for record.

id can have values from 1 - ....

Thanks

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 it's not a problem... you can keep your id, but using boolean for your checkbox... look for IValueConverter

in my case 

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int myValue = (int)value;

            if (myValue > 0)
            {
                return "n°" +myValue;
            }
            else
            {
                return "";
            }

        }

if my id >0, I add "n°" in my cell..... put the correct value (my ID) is again available in my objet....

(myObj.Id= 1 and not "n°1" )

in your case for example

 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int myId = (int)value;

            if (myValue > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

 hope this help you 

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

Sorry I tried the code, but it didn't work.

I think I do not understand much.

I implemented the IValueConverter in my countryxaml.vb that contains a datagrid with the checkbox.

Because I implemented an interface, I also put the 2 functions into my country.xaml.vb, I got the following errors

: Error    8    Argument not specified for parameter 'parameter' of 'Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object'.    C:\Users\Sharyn\Documents\Silverlight Practise\Enumaration\Enumaration\Views\Country.xaml.vb    90    60    Enumaration

:Error    4    Argument not specified for parameter 'parameter' of 'Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object'.    C:\Users\Sharyn\Documents\Silverlight Practise\Enumaration\Enumaration\Views\Country.xaml.vb    64    70    Enumaration

These errors seems the same to me and it repeated about 10 times.

 Here what I do not understand.

 How this function be executed? How this will communicate with the checkbox in datagrid?

Thanks ....

varshavmane
varshavmane

Contributor

Contributor

6721 points

1,579 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

Check this:

http://smehrozalam.wordpress.com/2009/04/03/ivalueconverters-a-great-tool-in-developing-wpfsilverlight-applications/

http://www.danielwoolston.com/?p=34

http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx

HTH Smile

Please "Mark as Answer" if this post answered your question. :)
Visit my Blog: http://varshavmane.blogspot.com/

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 ok slen so I'm going to explain more Smile

create a class ConvertCheckbox.cs in your projet

 

  public class ConvertCheckbox : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int myValue = (int)value;
            
            if (myValue >0)
            {
                return false;
            }
            else
            {
                return true;
            }
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
 add this reference to your xaml  xmlns:convert="clr-namespace:ProjectName.ConvertCheckbox" 
and  <UserControl.Resources>
<convert:ConvertCheckbox x:Key="modifCheckBox"/>
</UserControl.Resources>
 so next in your datagrid :
<data:DataGrid.Columns>
<data:DataGridCheckBoxColumn Header="ColumnName" Binding="{Binding ID, Converter={StaticResource modifCheckBox}}"/>
 
hope this help you !!
Johan 
-----
Please mark as answer if it help  
 
  

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

Thanks for the code. Sorry It didn't work for me though, but it definitely helps me in another thing somedays. 

Maybe I am not being clear enough.

if I am using the following code,

    <my:DataGridCheckBoxColumn Binding="{Binding id, Converter={StaticResource BoolConvertor}, Mode=TwoWay}" CellStyle="{StaticResource newCellStyle}"/>

 All the checkboxes were checked when first loaded..

If I used the cell template, the uncheck and check event are not being called. .. (not sure why it is )

 Actually, I want something that when I check the boxes of the grid, I will then determine to do something for it, for example delete. I would like to know how do I know which of the checkboxes is/are checked before the action delete comes to action. I want the user able to check and uncheck the checkboxes before any action is taken in place.

I don't know which way to solve this problem.

Any idea/thought is welcomed .. maybe there are things I missing or have never thought about 

 

Thank you

 

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

add a property IsChecked in your object which your are using in your projet....

when you create objects, set this property to false if ID is bad and true if it's correct !

Bind this property (IsChecked) to your datacheckboxcolumn... when user want to delete all the row wich are checked

foreach (ObjectType item in YourCollection)

{

 if (item.Ischecked) //add your action

}

slen
slen

Member

Member

10 points

59 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

How do I bind a property of IsChecked?

Do I write myown IsChecked function for DataGridCheckBoxColumn? 

There isn't any readOnly attribute in DataGridCheckBoxColumn?

 What is the Collection you mean there? I believe it is the datagrid, is it?

For Each item As Object In theDataGrid.ItemsSource

Next

I don't find any ObjectType in VB and I cannot convert the item to checkbox. 

just be patient ... I'm really not familiar with the whole idea here... sometimes, feeling bad to post so many questions , but I really want to explore the possibility to what I can do in silverlight ...

Thanks

 

koyot3
koyot3

Participant

Participant

805 points

172 Posts

Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Re: Checkbox event in datagrid cannot be detected..

 I guess your datagrid is binding with a List, an ObservableCollection or something else.

Check this link to help you to understand the binding / datagrid.

Your collection (your list) contains a lot of objects. You must work with your collection and not with your datagrid itemsource. When you modify your collection, the datagrid is updating with binding....

 For your checkcolumn, use the IsEnabled  property to lock this control....

The property IsChecked need a boolean value for the binding (true = check, false=uncheck)

for example, I create a class Class1.cs with two properties :

public string Name {get; set;}

public bool Answer {get; set;}

After, in my Page.xaml.cs, I create a lot of objet from Class1.cs like Class1 obj1 = new  Class1(), Class1 obj2 = new  Class1(),....

I add objects to a collection like ObservableCollection<Class1> mycollect; mycollect.Add(obj1).......

I bind this collection to my datagrid with itemsources and you can found in my xaml

 

 <data:DataGridTemplateColumn Header="Name" Width="300">
      <data:DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
           <TextBlock Text="{Binding Name}" />
           </DataTemplate>
       </data:DataGridTemplateColumn.CellTemplate>
  </data:DataGridTemplateColumn>

  <data:DataGridTemplateColumn Header="Answer">
       <data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
            <CheckBox  IsChecked="{Binding Answer}" />                        
            </DataTemplate>
       </data:DataGridTemplateColumn.CellTemplate>
  </data:DataGridTemplateColumn>

  hope this help you. Let me know if you have any problem with my code. It may be a little different in VB.Net (i'm using C#)

Johan

---------

Please mark as answer if it help

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities