We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Columns binding


Hi,

I created a custom control to be a base of others controls and this custom control has a SfDataGrid. 
I created custom dependency properties to provide a way to link the ItemSource and Columns properties for my control. 

The dependency property for ItemSource work very well, but the property for columns doesn't work.

What the correct way to implement that property? Bellow the part of code of my properties.

  • Dependency Properties
        // Colunas do Grid
        public Columns GridColmuns
        {
            get
            {
                return (Columns)GetValue(GridColmunsProperty);
            }
            set
            {
                SetValue(GridColmunsProperty, value);
            }
        }

        // Fonte de registros do Grid
        public object GridItemsSource
        {
            get
            {
                return (object)GetValue(GridItemsSourceProperty);
            }
            set
            {
                SetValue(GridItemsSourceProperty, value);
            }
        }

// Colunas do Grid
        public static readonly DependencyProperty GridColmunsProperty =
            DependencyProperty.Register("GridColmuns", typeof(Columns), typeof(ContentView));

        // Fonte de registros do Grid
        public static readonly DependencyProperty GridItemsSourceProperty =
            DependencyProperty.Register("GridItemsSource", typeof(object), typeof(ContentView),
                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


  • Custom control template
                           <sf:SfDataGrid
                                AllowFiltering="True"
                                AllowGrouping="True"
                                ColumnSizer="Star"
                                AutoGenerateColumns="False"
                                Columns="{Binding GridColmuns,RelativeSource={RelativeSource TemplatedParent}}"
                                ItemsSource="{Binding GridItemsSource, RelativeSource={RelativeSource TemplatedParent}}"
                                EnableDataVirtualization="True"
                                NavigationMode="Row"
                                ShowBusyIndicator="True"
                                ShowGroupDropArea="True"
                                CellStyle="{StaticResource GridCellStyle}"
                                RowStyle="{StaticResource GridRowControlStyle}"
                                HeaderStyle="{DynamicResource GridHeaderCellControlStyle}">
                            </sf:SfDataGrid>

5 Replies

SP Shobika Palani Syncfusion Team June 10, 2019 05:29 AM

Hi Tech, 

Thank you for contacting Syncfusion Support, 

We have analyzed your query related to column binding and we are able to reproduce the reported issue from our end. We are currently working on this with high priority  and we will update you with more details on 12th June,2019. 

We will appreciate your patience until then. 

Regards,
Shobika. 



SP Shobika Palani Syncfusion Team June 12, 2019 06:05 AM

Hi Tech, 
 
We regret for the inconvenience caused. 
 
Still we are validating the reported issue in our end and we need two more business days to finalize the behavior. We will update you with more details on 14th June,2019. 
 
We will appreciate your patience until then. 
 
Regards, 
Shobika. 



TS Tech Shop June 12, 2019 08:35 AM

Thank you


JP Jagadeesan Pichaimuthu Syncfusion Team June 13, 2019 03:16 AM

Hi Tech, 
 
Thanks for your update. 
 
We will update you with more details on June 14th, 2019.  
  
We will appreciate your patience until then. 
 
Regards, 
Jagadeesan 



SP Shobika Palani Syncfusion Team June 17, 2019 09:00 AM

Hi Tech, 

Thanks for your patience. 

We have analyzed your reported issue with binding columns when grid is loaded in ControlTemplate. And the reported issue can be resolved by binding columns via attached property. Please refer to the below code example 

C#: 
internal static class BindableColumns 
    { 
        public static Columns GetColumns(DependencyObject obj) 
        { 
            return (Columns)obj.GetValue(ColumnsProperty); 
        } 
 
        public static void SetColumns(DependencyObject obj, Columns value) 
        { 
            obj.SetValue(ColumnsProperty, value); 
        } 
 
        // Using a DependencyProperty as the backing store for Columns.  This enables animation, styling, binding, etc... 
        public static readonly DependencyProperty ColumnsProperty = 
            DependencyProperty.RegisterAttached("Columns", typeof(Columns), typeof(BindableColumns), new PropertyMetadata(null, OnColumnsPropertyChanged)); 
 
        public static void OnColumnsPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) 
        { 
            var dataGrid = obj as SfDataGrid; 
            if(dataGrid != null) 
                dataGrid.Columns = args.NewValue as Columns; 
        } 
    } 

XAML: 
<ControlTemplate> 
            <Grid> 
                <sf:SfDataGrid x:Name="grid"  
                                AllowFiltering="True" 
                                AllowGrouping="True" 
                                ColumnSizer="Star"  
                                AutoGenerateColumns="False"                                 
                                ItemsSource="{Binding GridItemsSource, RelativeSource={RelativeSource TemplatedParent}}" 
                                local:BindableColumns.Columns="{Binding GridColumns, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                EnableDataVirtualization="True" 
                                NavigationMode="Row" 
                                ShowBusyIndicator="True" 
                                ShowGroupDropArea="True"> 
                </sf:SfDataGrid> 
            </Grid> 
        </ControlTemplate> 

Also please find sample for the same from the below link 
Sample Link: 

Regards, 
Shobika. 


Loader.
Live Chat Icon For mobile
Up arrow icon