This question is locked. New answers and comments are not allowed.
I have a Column as such,
I need to be able to set the following property in code behind as opposed to doing it in XAML,
<telerik:GridViewDataColumn DataMemberBinding="{Binding Pre_Migration_Tasks}" Width="50" IsFilterable="False" IsSortable="False" IsReorderable="False" IsGroupable="False" CellStyleSelector="{StaticResource selector1}"> <telerik:GridViewDataColumn.Header> <Border Height="170" Margin="1,0,-151,0" > <TextBlock Text="Pre Migration Tasks" HorizontalAlignment="Left" FontSize="11" FontWeight="Normal" Height="auto" TextAlignment="Center" VerticalAlignment="Bottom" FontFamily="Portable User Interface" UseLayoutRounding="True" TextTrimming="None" Margin="9,0,0,0"> <TextBlock.RenderTransform> <RotateTransform Angle="270" /> </TextBlock.RenderTransform> </TextBlock> </Border> </telerik:GridViewDataColumn.Header> <telerik:GridViewDataColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding Pre_Migration_Tasks}" Checked="CheckBox_Click" IsThreeState="False" /> </DataTemplate> </telerik:GridViewDataColumn.CellTemplate> </telerik:GridViewDataColumn>I need to be able to set the following property in code behind as opposed to doing it in XAML,
Checked="CheckBox_Click"
How can I set this in code behind?
7 Answers, 1 is accepted
0
Timothy
Top achievements
Rank 1
answered on 18 Nov 2010, 12:00 AM
Please help,
How do I set these parameters in code behind as opposed to in XAML.
How do I set these parameters in code behind as opposed to in XAML.
<CheckBox IsChecked="{Binding Pre_Migration_Tasks}" Checked="CheckBox_Click"/>0
Hi Timothy,
Please follow the example below that demonstrates how to create such column:
MainPage.xaml
MainPage.xaml.cs
Considering your second question why do you need to define the DataTemplate in code behind?
The recommended approach is to define it in a ResourceCollecton and access it from code.
If you need any further assistance please let me know.
Best wishes,
Vanya Pavlova
the Telerik team
Please follow the example below that demonstrates how to create such column:
MainPage.xaml
<UserControl xmlns:telerikGrid="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.GridView" xmlns:telerik="http%3A%2F%2Fschemas%2Etelerik%2Ecom/2008/xaml/presentation" x:Class="SilverlightApplication197.MainPage" Width="640" Height="480"> <UserControl.Resources> <DataTemplate x:Key="dt"> <CheckBox IsChecked="{Binding IsMarried}" Checked="CheckBox_Checked" IsThreeState="False" /> </DataTemplate> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="32"/> </Grid.RowDefinitions> <telerik:RadGridView x:Name="grid" AutoGenerateColumns="False"/> <telerik:RadButton x:Name="radButton1" Grid.Row="1" Content="Create Duplicate Column" Click="radButton1_Click"/> </Grid> </UserControl>MainPage.xaml.cs
public partial class MainPage : UserControl { public MainPage() { // Required to initialize variables InitializeComponent(); this.grid.ItemsSource = (from c in Enumerable.Range(0, 10) select new Employee { IsMarried = false }).ToList(); } private void radButton1_Click(object sender, RoutedEventArgs e) { GridViewDataColumn dataColumn1 = new GridViewDataColumn(); //Binding is a Boolean property similar to tne one from your example dataColumn1.DataMemberBinding = new System.Windows.Data.Binding("IsMarried"); dataColumn1.Width = 50; dataColumn1.IsFilterable = false; dataColumn1.IsSortable = false; dataColumn1.IsGroupable = false; dataColumn1.IsReorderable = false; //The grid is preferrable due to its flexibility Grid border = new Grid(); border.Height = 170; border.Margin = new Thickness(1, 0, -151, 0); TextBlock textBlock = new TextBlock(); textBlock.Text = "Pre Migration Tasks"; textBlock.VerticalAlignment = VerticalAlignment.Bottom; textBlock.HorizontalAlignment = HorizontalAlignment.Left; textBlock.FontSize = 11; textBlock.FontWeight = FontWeights.Normal; textBlock.TextAlignment = TextAlignment.Center; textBlock.UseLayoutRounding = true; textBlock.TextTrimming = TextTrimming.None; textBlock.Margin = new Thickness(9,0,0,0); RotateTransform transfrom = new RotateTransform(); transfrom.Angle = 270; textBlock.RenderTransform = transfrom; border.Children.Add(textBlock); dataColumn1.Header = border; //Here the CellStyleSelector is a custom class you can also define it in xaml and set it //in a way smilar to the one demonstrated for datacolumn1's CellTemplate dataColumn1.CellStyleSelector = new CellStyleSelector(); dataColumn1.CellTemplate = this.Resources["dt"] as DataTemplate; this.grid.Columns.Add(dataColumn1); } private void CheckBox_Checked(object sender, RoutedEventArgs e) { MessageBox.Show("I am Checked"); } } public class Employee { public bool IsMarried { get; set; } }Considering your second question why do you need to define the DataTemplate in code behind?
The recommended approach is to define it in a ResourceCollecton and access it from code.
If you need any further assistance please let me know.
Best wishes,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Timothy
Top achievements
Rank 1
answered on 18 Nov 2010, 08:39 AM
Perfect. I was running in circles and this is exactly what I was looking for. Thank you so much!!!. You guys have the best customer service I have ever seen. Simply fantastic!.
I have one other question.
when my gridview loads I need to disable any checkboxes that load with a NULL value. Currently they load with a minus sign through them and I really need them to come up disabled if they are null. What is the best way to accomplish this requirement?
Thanks again.
Tim
I have one other question.
when my gridview loads I need to disable any checkboxes that load with a NULL value. Currently they load with a minus sign through them and I really need them to come up disabled if they are null. What is the best way to accomplish this requirement?
Thanks again.
Tim
0
Hi Timothy,
You can easily bind IsEnabled property to a boolean property and using a converter you can check if the value is null it should return False and the CheckBox will be disabled, otherwise it will return the corresponding value either True or False.
You should change a little bit the DataTemplate in this way:
The code for the Converter1 is pretty simple:
As you might noticed the Checked state in DataTemplate is missing, this is due to the fact you should find all CheckBoxElements in RadGridView's RowLoadedEvent and attach the event handler:
Now if the value is null the CheckBox is being disabled and when a CheckBox is checked the event is being executed. But the minus is still there, finally I would suggest you to edit the template of the CheckBox and delete the Rectangle that is used in its Indeterminate state and apply this style in the defined DataTemplate.
Regards,
Vanya Pavlova
the Telerik team
You can easily bind IsEnabled property to a boolean property and using a converter you can check if the value is null it should return False and the CheckBox will be disabled, otherwise it will return the corresponding value either True or False.
You should change a little bit the DataTemplate in this way:
<UserControl.Resources> <local:Converter1 x:Key="con"/> <DataTemplate x:Key="dt"> <CheckBox IsChecked="{Binding Married}" IsEnabled="{Binding Married,Converter={StaticResource con}}"/> </DataTemplate> </UserControl.Resources> .... </UserControl>The code for the Converter1 is pretty simple:
public class Converter1:IValueConverter { object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Nullable<bool> item = (Nullable<bool>)value; if (item != null) { return true; } return false; } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }As you might noticed the Checked state in DataTemplate is missing, this is due to the fact you should find all CheckBoxElements in RadGridView's RowLoadedEvent and attach the event handler:
private void CheckBox_Checked(object sender, RoutedEventArgs e) { MessageBox.Show("I am Checked"); } private void grid_RowLoaded(object sender, RowLoadedEventArgs e) { var row = e.Row as GridViewRow; if (row != null) { var checkBoxes = row.ChildrenOfType<CheckBox>().ToList(); foreach (var c in checkBoxes) { c.Checked+=new RoutedEventHandler(CheckBox_Checked); } } } Now if the value is null the CheckBox is being disabled and when a CheckBox is checked the event is being executed. But the minus is still there, finally I would suggest you to edit the template of the CheckBox and delete the Rectangle that is used in its Indeterminate state and apply this style in the defined DataTemplate.
Regards,
Vanya Pavlova
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Timothy
Top achievements
Rank 1
answered on 20 Nov 2010, 08:26 AM
Is there anyway to implement threestate checkboxes that provide for NULL, True, and False, WITHOUT using cell templates. The implmentation of the cell templates into the grid has caused a significant performance issue. Thegrid takes about 30 seconds to load with virtualization on and about a minute with it off. I am guessing this is due to the fact that it has to analyze all the cells (about 1800) to determine Null, True, False and apply the applicable style.
Is there anyway to improve performance in this case?
Is there anyway to improve performance in this case?
0
Hi Timothy,
You can achieve this by setting GridViewCheckBoxColumn.IsThreeState to true and you will not need to define any CellTemplates.
Best wishes,
Ivan X1
the Telerik team
You can achieve this by setting GridViewCheckBoxColumn.IsThreeState to true and you will not need to define any CellTemplates.
Best wishes,
Ivan X1
the Telerik team
Browse the videos here>> to help you get started with RadControls for Silverlight
0
Timothy
Top achievements
Rank 1
answered on 02 Dec 2010, 08:28 PM
I am now using a custom column class as below,
I cannot figure out how to configure the checkbox click event. I was previously setting it up in a datatemplate for the column but now that I am using a custom column I dont know what to do.
What can I do to call a routine when a checkbox is checked?
Thanks,
Tim
Imports System Imports System.Net Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Documents Imports System.Windows.Ink Imports System.Windows.Input Imports System.Windows.Media Imports System.Windows.Media.Animation Imports System.Windows.Shapes Imports Telerik.Windows.Controls Imports Telerik.Windows.Controls.GridView Imports System.Windows.Controls.Primitives Public Class CustomColumn Inherits GridViewCheckBoxColumn Public Overrides Function CreateCellElement(ByVal cell As GridViewCell, ByVal dataItem As Object) As FrameworkElement Dim cb = TryCast(cell.Content, CheckBox) If cb Is Nothing Then cb = New CheckBox() cb.Height = 20 cb.SetBinding(CheckBox.IsCheckedProperty, Me.DataMemberBinding) cell.Content = cb cb.IsThreeState = True cell.SetBinding(GridViewCell.ValueProperty, Me.DataMemberBinding) End If If cell.Value = "True" Then cell.Background = New SolidColorBrush(Colors.Green) End If Return cb End FunctionEnd ClassI cannot figure out how to configure the checkbox click event. I was previously setting it up in a datatemplate for the column but now that I am using a custom column I dont know what to do.
What can I do to call a routine when a checkbox is checked?
Thanks,
Tim