The checking of these boxes will have very similar functionality so, is there a way were I can tell which checkbox was checked so I can just have one event.
The functionality will just be: If checkbox1 is checked update fields 2 and 3, if checkbox2 is checked update fields 8 and 9.
Hope this makes sense.
KS.
12 Answers, 1 is accepted
You can give these CheckBoxes names and in the event you can cast the sender to CheckBox and and check the current CheckBox Name.
Kind regards,Vlad
the Telerik team
One more question. When my page loads the gridview is empty until the user selects a name.
But when the gridview loads the _Checked(object sender, RoutedEventArgs e) event fires 2 times for each of the checkboxes I have.
Then when I check one of them, the event fires multiple times.
I have this code:
private void chkOverRide_Checked(object sender, RoutedEventArgs e) { CheckBox chk = (CheckBox)sender; string chkName = chk.Name; var row = chk.ParentOfType<GridViewRow>(); AATshipAgree agrow = new AATshipAgree(); agrow = (AATshipAgree)row.DataContext; if (chkName == "chkCostOverRide") { if (row != null) { row.Cells[10].IsEnabled = true; row.Cells[12].IsEnabled = true; // row.Cells[14].IsEnabled=true; } } else if (chkName == "chkHazOverRide") { if (row != null) { row.Cells[18].IsEnabled = true; row.Cells[20].IsEnabled = true; // row.Cells[22].IsEnabled = true; } } }It works until it reaches the commented out row.Cells. Cells 14, and 22. These cells are the cells right after the checkbox cell. So it seems that once it hits this cell it calls the Checked event. I need this event to be called after the row is complete. The cells that I need to enable or disable are after the check box.
Thanks,
KS
I am wanting to enable and disable cells based on checking and unchecking the checkbox. Apparently what I have is not working.
Can a sample be given.
Thanks,
KS
The code I included in the other reply does not work as I had thought.
Thanks,
KS
I have prepared a sample project implementing the code-snippet you have provided and everything works as expected. You may use it as a reference and in case there is some misunderstanding according to your requirements, you may change the application in the way you want and send it back.
Maya
the Telerik team
And in my project the checked event works fine, but the unchecked event, for some reason the object
AATshipAgree agrow = new AATshipAgree();
agrow = row.DataContext as AATshipAgree;
is blank.
private void chkOverRide_Checked(object sender, RoutedEventArgs e) { CheckBox chk = (CheckBox)sender; string chkName = chk.Name; var row = chk.ParentOfType<GridViewRow>(); AATshipAgree agrow = new AATshipAgree(); agrow = row.DataContext as AATshipAgree; if (chkName == "checkBox1") { if (row != null) { row.Cells[10].IsEnabled = true; row.Cells[12].IsEnabled = true; row.Cells[14].IsEnabled = true; } } else if (chkName == "checkBox2") { if (row != null) { row.Cells[18].IsEnabled = true; row.Cells[20].IsEnabled = true; row.Cells[22].IsEnabled = true; } } } private void chkCostOverRide_Unchecked(object sender, RoutedEventArgs e) { CheckBox chk = (CheckBox)sender; string chkName = chk.Name; var row = chk.ParentOfType<GridViewRow>(); AATshipAgree agrow = new AATshipAgree(); agrow = row.DataContext as AATshipAgree; if (chkName == "checkBox1") { if (row != null) { row.Cells[10].IsEnabled = false; row.Cells[12].IsEnabled = false; row.Cells[14].IsEnabled = false; } } else if (chkName == "checkBox2") { if (row != null) { row.Cells[18].IsEnabled = false; row.Cells[20].IsEnabled = false; row.Cells[22].IsEnabled = false; } } }Let me know if you see anything I might be doing wrong.
Thanks,
KS
I have handled the Unchecked event of the CheckBox-es in the sample project I sent to you, but unfortunately I am not able to reproduce it. Thus in order to provide you with any further solution, I would need a bit more information about your project, the grid's ItemsSource and the way it is set. Furthermore, what is the exact implementation and definition of your AATshipAggree class?
Maya
the Telerik team
Now I have just my checked event and the unchecked event is commented out. And with this code I see that the checked event gets called 2 times. The first time it goes throught and disables my cells but then the second time row.datacontent is null and throws a null error. The times when I had it working was when I had commented out the code for cells 14, and 22 because these would return an out of range error.
private void chkOverRide_Checked(object sender, RoutedEventArgs e) { CheckBox chk = (CheckBox)sender; string chkName = chk.Name; var row = chk.ParentOfType<GridViewRow>(); AATshipAgree agrow = new AATshipAgree(); agrow = (AATshipAgree)row.DataContext; if (chkName == "chkCostOverRide") { if (row != null) { row.Cells[10].IsEnabled = true; row.Cells[12].IsEnabled = true; //row.Cells[14].IsEnabled = true; } } else if (chkName == "chkHazOverRide") { if (row != null) { row.Cells[18].IsEnabled = true; row.Cells[20].IsEnabled = true; // row.Cells[22].IsEnabled = true; } } }I just created a bool property, and set then to your GetClubs method.
If any of the players have a true for this new bool, then the app breaks.
public static ObservableCollection<Club> GetClubs() { ObservableCollection<Club> clubs = new ObservableCollection<Club>(); Club club; // Liverpool club = new Club("Liverpool", new DateTime(1892, 1, 1), 45362); //club.players.Add(new Player("test",99,Position.DF,"England",true)); club.Players.Add(new Player("Pepe Reina", 25, Position.GK, "Spain", false)); club.Players.Add(new Player("Jamie Carragher", 23, Position.DF, "England",false)); club.Players.Add(new Player("Steven Gerrard", 8, Position.MF, "England",true)); club.Players.Add(new Player("Fernando Torres", 9, Position.FW, "Spain",false)); clubs.Add(club); // Manchester Utd. club = new Club("Manchester Utd.", new DateTime(1878, 1, 1), 76212); club.Players.Add(new Player("Edwin van der Sar", 1, Position.GK, "Netherlands", false)); club.Players.Add(new Player("Rio Ferdinand", 5, Position.DF, "England", true)); club.Players.Add(new Player("Ryan Giggs", 11, Position.MF, "Wales", true)); club.Players.Add(new Player("Wayne Rooney", 10, Position.FW, "England", true)); clubs.Add(club); // Chelsea club = new Club("Chelsea", new DateTime(1905, 1, 1), 42055); club.Players.Add(new Player("Petr Cech", 1, Position.GK, "Czech Republic", true)); club.Players.Add(new Player("John Terry", 26, Position.DF, "England", true)); club.Players.Add(new Player("Frank Lampard", 8, Position.MF, "England", true)); club.Players.Add(new Player("Nicolas Anelka", 39, Position.FW, "France", true)); clubs.Add(club); // Arsenal club = new Club("Arsenal", new DateTime(1886, 1, 1), 60355); club.Players.Add(new Player("Manuel Almunia", 1, Position.GK, "Spain", true)); club.Players.Add(new Player("Gaël Clichy", 22, Position.DF, "France", true)); club.Players.Add(new Player("Cesc Fà bregas", 4, Position.MF, "Spain", true)); club.Players.Add(new Player("Robin van Persie", 11, Position.FW, "Netherlands", true)); clubs.Add(club); return clubs; }using System.ComponentModel; using System.Collections.ObjectModel; using System.Linq; namespace CheckBox_EnableCells { /// <summary> /// A football player. /// </summary> public class Player : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string name; private int number; private Position position; private string country; private bool active; public string Name { get { return this.name; } set { if (value != this.name) { this.name = value; this.OnPropertyChanged("Name"); } } } public int Number { get { return this.number; } set { if (value != this.number) { this.number = value; this.OnPropertyChanged("Number"); } } } public Position Position { get { return this.position; } set { if (value != this.position) { this.position = value; this.OnPropertyChanged("Position"); } } } public string Country { get { return this.country; } set { if (value != this.country) { this.country = value; this.OnPropertyChanged("Country"); } } } public bool Active { get { return this.active; } set { if (value != this.active) { this.active = value; this.OnPropertyChanged("Active"); } } } public Player() { } public Player(string name, int number, Position position, string country, bool active) { this.name = name; this.number = number; this.position = position; this.country = country; this.active = active; } protected virtual void OnPropertyChanged(PropertyChangedEventArgs args) { PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { handler(this, args); } } private void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public override string ToString() { return this.Name; } public static ObservableCollection<Player> GetPlayers() { return new ObservableCollection<Player>(Club.GetClubs().SelectMany(c => c.Players)); } } } And in the xaml I am setting
<DataTemplate> <CheckBox IsChecked="{Binding Active}" x:Name="checkBox1" Unchecked="checkBox1_Unchecked" Checked="checkBox1_Checked" /> </DataTemplate>Do I need to bind differntly, instead of using ischecked?
I am binding the checkbox to a boolean value so if it's true I want to disable cells and if it's false I leave them enabled.
I just hope the client is fine with me reagranging the cells.
KS.
The problem in the case when setting a Property bound to IsChecked of the CheckBox is that not all of the cells are rendered during the initial firing of the Checked event. However, as an appropriate workaround, you may use the IsReadOnlyBinding Property of the grid. It sets a particular cell to be ReadOnly or not. Thus by means of a IValueConverter, you may connect the value of that property to the Active Property of the Player class.
I am sending you a sample project illustrating the proposed solution.
Maya
the Telerik team
