Create GridTemplateColumn programmatically
Hi,
I am currently generating columns for my SfDataGrid, which is binding to a DataTable, and I am facing some problems when it is a GridTemplateColumn.
I have created a UserControl for my template as following
CustomGridCell.xaml and code behind as following:
<UserControl ...>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="TextBlock"/>
</StackPanel>
</UserControl>
public partial class CustomGridCell : UserControl
{
public CustomGridCell()
{
InitializeComponent();
}
public object Value
{
get { return GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(object),
typeof(CustomGridCell), new PropertyMetadata(default, ValueChanged));
private static async void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as CustomGridCell;
var context = control.DataContext as DataTable;
var value = await ValuesUtils.GetValueAsync(e.NewValue, context);
control.TextBlock.Text = value?.ToString();
}
}
The problem is when the code reaches ValueChanged method, the control.DataContext is null so I am not able to generate the values.
The way I am generating the TemplateColumn is this:
FrameworkElementFactory element = new FrameworkElementFactory(typeof(CustomGridCell));
var bind = new Binding
{
Path = new PropertyPath(column.Name),
Mode = BindingMode.TwoWay
};
element.SetBinding(CustomGridCell.ValueProperty, bind);
var dataTemplate = new DataTemplate(typeof(CustomGridCell));
dataTemplate.VisualTree = element;
var column = new GridTemplateColumn
{
HeaderText = column.HeaderText,
MappingName = column.Name,
FilterBehavior = FilterBehavior.StronglyTyped,
CellTemplate = dataTemplate
};
How can I manage to set the correct context to my custom cell control?
Hi Álvaro Sánchez López,
Thanks for the detailed information and the code snippet, but we have a concern regarding the provided code snippet.
We have reviewed the provided information and code snippet. You have created a custom user control and loaded that control in the cell template for the GridTemplateColumn. Additionally, you have defined the binding for the Value property and the Name property of the 'column' object. As we can see in the code snippet, the GridTemplateColumn was created with the same name as you have defined for the object "column". However, you have used that object 'column' earlier for the binding. We are a bit unclear about this. Please refer to the image reference below.
To address this, we have created a class with HeaderText and Name properties and used it for binding. Once the SfDataGrid was loaded, the ValueChanged method was executed. Inside that method, control.DataContext received a value that was not null. Please refer to the image reference below.
Also, could you please provide the following information?
- What DataContext have you set for the SfDataGrid?
- Could you please provide more details about the 'column' object that we have highlighted in the above image?
This information will help us replicate the issue and provide a valid solution.
We have attached the sample that we have tested for your reference.
Regards,
Sreemon Premkumar M.
Attachment: SfDataGrid_WPF_Demo_81195b98.zip
Hi Sreemon, thanks for the answer.
My code snipped had a mistake. I tried to simplify the code for the post and I assigned the template to a variable but it was a 'return' instruction in my real code.
Answering to your questions:
- What DataContext have you set for the SfDataGrid?
It is a DataTable as the sample you provide. - Could you please provide more details about the 'column' object that we have highlighted in the above image?
Column object is an own model we use to map the third party columns (in that case TemplateColumn) from. It only have data about text, sizes, etc. The sample you provide works as similar.
Anyways, get realized that I do not need the DataContext so my real problem is solved but could be great to know why it is null.
Hi Álvaro Sánchez López,
Thanks for the update.
The reason for the DataContext being null is that, when scrolling, we did not create a new object for the row. Instead, we reused the row that moved out of view (i.e., the first row) for the row that comes into view when scrolling. Before assigning new values, we reset the values of that object. This causes the DataContext to be null. However, you will get the DataContext once the row is created. Therefore, you can simply check for a null condition before assigning the DataContext.
Regards,
Sreemon Premkumar M.
- 3 Replies
- 2 Participants
-
ÁS Álvaro Sánchez López
- Aug 1, 2024 05:52 PM UTC
- Aug 5, 2024 01:37 PM UTC