Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Checkbox event in datagrid cannot be detected..
23 replies. Latest Post by koyot3 on July 9, 2009.
(0)
slen
Member
10 points
59 Posts
07-03-2009 4:25 AM |
<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
Contributor
6721 points
1,579 Posts
07-03-2009 5:00 AM |
Hi,
It works fine with me. Here is the code which that I used:
<
Code Behind:
dataGridCategories.ItemsSource = categories;
{
}
I guess the problem might be with Itemssource. Please check that.
HTH
koyot3
Participant
805 points
172 Posts
07-03-2009 5:07 AM |
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.
07-05-2009 10:54 AM |
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?
07-05-2009 11:02 AM |
My Itemsource seems good to me. The source is assigned to the datagrid once the page get loaded.
07-05-2009 11:03 AM |
07-06-2009 1:36 AM |
Did you check with the code that I have posted ?
07-06-2009 4:06 AM |
Yes. It is almost the same as mine. The thing that is different is that my itemsource is assigned thought a web service called.
07-06-2009 4:22 AM |
That doesnt make any difference.
Can you post your WCF code ?
07-06-2009 9:14 PM |
The following code is the part from WCF.
Private Sub Page_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
webService = New ServiceReference2.ServiceCountryClientAddHandler 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
07-07-2009 1:08 AM |
What you get in e.Result ?
07-07-2009 2:50 AM |
Is the collection of data. I assigned to source because I want to store that object. It returns ObservableCollection object ...
Thannks
07-07-2009 2:54 AM |
you can add :
myDatagrid.ItemsSource = null before the myDatagrid.ItemsSource = myCollection...
what's the type of your "Id" property ??
07-07-2009 3:48 AM |
The Id is in integer type ...
I tried it setting it to null first .. but it didn't work ..
07-07-2009 3:55 AM |
I'ts better for a check box to bind with a boolean.....
you can use an IValueConverter to return the good value ....
07-07-2009 4:49 AM |
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 - ....
07-07-2009 5:00 AM |
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
07-08-2009 12:06 AM |
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 ....
07-08-2009 1:13 AM |
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
07-08-2009 2:31 AM |
ok slen so I'm going to explain more
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
07-08-2009 5:25 AM |
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
07-08-2009 5:46 AM |
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
07-08-2009 10:28 PM |
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 ...
07-09-2009 2:29 AM |
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#)
---------