Posted 15 June 2018, 4:47 am EST
Hi
I need to set the flexgrid column header through binding,
Is it possible somehow?
Thanks
Forums Home / ComponentOne / WPF Edition
Posted by: kisar on 15 June 2018, 4:47 am EST
Posted 15 June 2018, 4:47 am EST
Hi
I need to set the flexgrid column header through binding,
Is it possible somehow?
Thanks
Posted 18 June 2018, 5:13 am EST
Hi,
For binding Column Header in C1FlexGrid, please use following code snippet:
<c1:C1FlexGrid.Columns>
<c1:Column IsReadOnly="False">
<c1:Column.HeaderTemplate>
<DataTemplate>
[b] <TextBlock Text="{Binding Source={x:Reference textBox}, Path= Text}"/>[/b]
</DataTemplate>
</c1:Column.HeaderTemplate>
</c1:Column>
</c1:C1FlexGrid.Columns>
```Also, attached is sample application for your reference.
Thanks,
Ruchir
[zip filename="BindColumnHeaderWPF.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-6672e342-cc46-450e-9dc1-a2ce43a40a17.zip[/zip]
Posted 19 June 2018, 4:24 am EST
Hi, thanks for the answer
but i would like to ask you id ther’s a more compact manner to have this result,
because the grid must be used ery often in the project.
With your solution i need to write and customize too much for every grid, It would be very efficient to have one solution like this:
c1:C1FlexGrid.Columns
<c1:Column Header=“Binding {kbinding_language.kgrid_col_id}” Width=“60” Binding=“{Binding krecord_base.kid}”/>
<c1:Column Header=“Binding {kbinding_language.kgrid_col_companyname}” Width=“3*” Binding=“{Binding krecord_base.kname}”/>
</c1:C1FlexGrid.Columns>
I tried also to make i my flexgrid : MY_column_wpf : C1.WPF.FlexGrid.Column
and add there a DependencyProperty but it is not possible because C1.WPF.FlexGrid.Column is not a DependencyObject
Do you have more ideas for me?
Thanx
Posted 20 June 2018, 2:24 am EST
Hi,
To bind the column header, you have to manually place a TextBlock in HeaderTemplate and then bind it’s Text to any property.```
<c1:C1FlexGrid.Columns>
<c1:Column Binding="{Binding ID, Mode=TwoWay}">
<c1:Column.HeaderTemplate>
<DataTemplate>
[b]<TextBlock Text="{Binding ColId}"/>[/b]
</DataTemplate>
</c1:Column.HeaderTemplate>
</c1:Column>
<c1:Column Binding="{Binding FName}">
<c1:Column.HeaderTemplate>
<DataTemplate>
[b] <TextBlock Text="{Binding ColName}"/>[/b]
</DataTemplate>
</c1:Column.HeaderTemplate>
</c1:Column>
<c1:Column Binding="{Binding LName}" />
</c1:C1FlexGrid.Columns>
Thanks,
Ruchir
[img]https://gccontent.blob.core.windows.net/forum-uploads/file-c7552b50-fe1a-41a3-ba88-bdf266c81bdd.png[/img]
[zip filename="BindColumnHeader.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-c3d3585a-8872-4392-9328-5f1369c4ddd6.zip[/zip]
Posted 20 June 2018, 3:04 am EST
Yes, i understood the previous example, this is the same. I was hoping could be possibile have a different more compact solution.
If, in the future, could be possible to have the Header as DependencyProperty it would be very usefull
Thanx
Posted 20 June 2018, 8:40 am EST
Sorry, im using your solution, can you help me to recovery the textblock by codebehind.
I need to check at runtime the text value of the textblock in the header template.
Thanx
Posted 22 June 2018, 12:52 am EST
Hi,
To access the value of binded column header, you need to take help of CellFactory’s CreateColumnHeaderContent method and inside get the value using BindingExpression, as follows:```
public override void CreateColumnHeaderContent(C1.WPF.FlexGrid.C1FlexGrid grid, Border bdr, C1.WPF.FlexGrid.CellRange range)
{
base.CreateColumnHeaderContent(grid, bdr, range);
if (grid.Columns[range.Column].DataType == typeof(string) || grid.Columns[range.Column].DataType == typeof(int)) //get columns of specific type or by index etc
{
var tb = bdr.Child as TextBlock;
if(tb.Text!="")
{
Console.WriteLine($"UnBound Column Header text: {tb.Text}");
}
if (tb != null)
{
tb.Background = Brushes.Red;
BindingExpression xp = tb.GetBindingExpression(TextBlock.TextProperty);
if (xp != null)
{
string bindingPath = xp.ParentBinding.Path.Path;
string[] properties = bindingPath.Split('.');
object value = _colBinding.GetType().GetProperty(properties.Last<string>()).GetValue(_colBinding);
Console.WriteLine($"Binded Column Header text: {value}");
}
}
}
}
Thanks,
Ruchir
[zip filename="BindColumnHeaderWPF_Get.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-22ba9ad8-f66b-4843-a5bc-76b01927f210.zip[/zip]
Posted 23 June 2018, 3:21 am EST
Hi i cannot open the zip file, can you resend it.
However my purpose is to find the column from its header text.
So first i set the column header in binding with your solution, after at runtime i need to know the column with a specific text header.
Thanx
Posted 26 June 2018, 4:31 am EST
Hi,
I have reattached the file for you to test. Also, I tweaked the code a little to show column index,Name containing specific header text.
Hope it meets your requirement.
Thanks,
Ruchir
BindColumnHeaderWPF1.zip
Posted 2 July 2018, 1:57 am EST
Thanx