Need help with binding of DataFormDropDownItem

Hi,
I found your dataformdropdownitem-binding and I'm trying to achieve something similar. In the sample project the BindingContext of the dropDownItem is set to a static resource. This is working fine. But I'm trying to bind ItemsSource to an ObservableCollection on my ViewModel. This does not work.
I'm new to Xamarin and I think I just do not understand the binding/binding context internals.




The Contacts.Country TextItem works fine, while Contacts.Clients is empty. I also tried to bind to Clients and tried several BindingContext options but it just does not work.
I attached my modified sample project. Thank you!

Attachment: DataFormXamarin5224268982_355b0df3.zip

4 Replies 1 reply marked as answer

SS SaiGanesh Sakthivel Syncfusion Team April 28, 2021 01:47 PM UTC

Hi Andre, 
 
Thank you for using syncfusion support. 
 
#Regarding Binding of DataFormDropDownItem 
we have checked the reported query from our end. we suggest to you to set the DataFormDropDownItem ItemSource collection property in the viewmodel instead of a model class and also set the BindingContext for the DataFormDropDownItem to achieve your requirement. Please refer to the following code snippet for your reference. 
 
Code snippet 
<ContentPage.BindingContext> 
    <viewModel:DataFormViewModel x:Name="DataFormViewModel" /> 
</ContentPage.BindingContext> 
 
    <ContentPage.Content> 
        <Grid> 
            <dataForm:SfDataForm x:Name="dataForm" DataObject="{Binding Contacts}" LayoutOptions="TextInputLayout" AutoGenerateItems="false"> 
                <dataForm:SfDataForm.Items> 
                    <dataForm:DataFormDropDownItem Name="SelectedClient" 
                                            Editor="DropDown" 
                                            BindingContext="{x:Reference DataFormViewModel}" 
                                            ItemsSource="{Binding Clients}" 
                                            DisplayMemberPath="Name"   
                                            SelectedValuePath="Code" /> 
                    <dataForm:DataFormTextItem Name="Country" Editor="Text"/> 
                </dataForm:SfDataForm.Items> 
            </dataForm:SfDataForm> 
        </Grid> 
    </ContentPage.Content> 
 
ViewModel Class 
private ObservableCollection<Client> clients; 
        public ObservableCollection<Client> Clients 
        { 
            get => clients; 
            set 
            { 
                clients = value; 
                RaiseOnPropertyChanged(nameof(Clients)); 
            } 
        } 
        public DataFormViewModel() 
        { 
            contacts = new DataFormModel(); 
            Clients = new ObservableCollection<Client> { new Client { Name = "TestClient", Code = "TestCode" } }; 
        } 
 
Please refer to the following tested sample for your reference. 
 
Please let us know if you have any concern. 
 
Regards, 
SaiGanesh Sakthivel

Marked as answer

AS Andre Smolarczyk April 28, 2021 05:52 PM UTC

Thank you!

Your sample code is working fine, but I think it does not really solve my problem.
In the sample app I manually create a DataFormViewModel ViewModel and assign it as BindingContext of the DataFormPage. With your code a second DataFormViewModel is instantiated in the view/page. For the sample this is fine.

But actually I'm using prism framework to bind a ViewModel to a View/Page. The ViewModel uses EF Core to load data for the dropdowns and there are some dependencies between properties/dropdowns. Therefore creating two ViewModel instances does not feel right.


SS SaiGanesh Sakthivel Syncfusion Team April 30, 2021 04:13 AM UTC

Hi Andre,  
  
Thank you for the update.  
  
#Regarding Need help with binding of DataFormDropDownItem 
Currently, we are checking the reported scenario and we will update the further details on or before the May 3,2021. We appreciate your patience until then.  
  
Regards,  
SaiGanesh Sakthivel 



IR Indumathi Ravichandran Syncfusion Team May 4, 2021 03:09 PM UTC

Hi Andrew, 
 
Thank you for your patience. 
 
Regarding Query: Regarding Need help with binding of DataFormDropDownItem  
 
Based on the last update we have checked the mentioned issue and as per the SfDataForm implementation DataFormDropDownItem is a class, and it does not inherit the BindingContext from ViewModel. So, we need to provide X reference to get the data for DropDownEditor. Otherwise, you can set the data in code behind in OnAutoGeneratingEvent. 
 
We hope that above information helps you. Please let us know if you need further assistance. 
 
Regards, 
Indumathi R 


Loader.
Up arrow icon