Articles in this section
Category / Section

How to load the collection in GridComboBoxColumn when defining DataContext after loading Page?

1 min read

In SfDataGrid, the GridComboBoxColumn will loads the collection when defining DataContext before loading the SfDataGrid. When we defining the DataContext in Page.Loaded event, the binding will not updated for GridComboBoxColumn which loads the empty collection. But you can achieve this by using DependencyObject class like below code example.


C#

WPF

this.Loaded += MainPage_Loaded;
 
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    this.DataContext = new ViewModel();
}
 
 
public class BindingProxy: DependencyObject
{
    public BindingProxy() { }
 
    //Dependency property Data for holding ComboBox ItemsSource value.
            
    public object Data
      {
        get { return (object)GetValue(DataProperty); }
             set  { SetValue(DataProperty, value); }
    }
       
    // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new PropertyMetadata(null));  
}

 

 

XAML           

<Page.Resources>
        <local:BindingProxy x:Key="proxy" Data="{Binding}" />    
</Page.Resources>
 
<syncfusion:SfDataGrid x:Name="DataGrid"
                               AutoGenerateColumns="False"
                               ItemsSource="{Binding Orderlist}"
                               NavigationMode="Cell">
            <syncfusion:SfDataGrid.Columns>
                <syncfusion:GridTextColumn MappingName="Name" />
                <syncfusion:GridTextColumn MappingName="City" />
                <syncfusion:GridTextColumn MappingName="Country" />
                <syncfusion:GridComboBoxColumn AllowEditing="True"
                                               DisplayBinding="{Binding Path=SelectedName}"
                                               DisplayMemberPath="Name"
                                               HeaderText="ComboBox-Column"
                                               ItemsSource="{Binding Data.StringName,
                                                                     Source={StaticResource proxy}}" />
            </syncfusion:SfDataGrid.Columns>
        </syncfusion:SfDataGrid>

 

Sample Link

 

UWP

 

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied