Tech Off Thread

13 posts

Dynamic Binding for TextBlock inside of DataTemplate in ComboBox

Back to Forum: Tech Off
  • santrajas

    I am trying to Bind the TextBlock inside DataTemplate of ComboBox through Code. 

    XAML Code : 

    <ComboBox Name="cmbRefDoctor">
                        <TextBlock x:Name="txtRefDoctorName" />

    Binding for the TextBlock "txtRefDoctorName" should be made in the code and not in the XAML. I am able to bind all the other controls which are not in DataTemplate but I am not able to solve this problem. 

    Can you please suggest a solution.

  • spivonious

    @santrajas: You don't have a property name to bind to in XAML? You can do that, and then set the DataContext of the ComboBox in code.

    If you don't know the property name at design time, then DataTemplates really aren't the answer.

  • santrajas

    @spivonious I do know the property name to be used for binding.

    But I want to bind the Textblock in code and not in XAML. That is the requirement. 

    using DataTemplate is not mandatory for me, but since I am quite new to Silverlight don't know other option to be used.

    Can you suggests somethings, may be a hint or idea.



  • spivonious

    @santrajas: By putting the property name in the XAML, you set everything up beforehand. The actual binding doesn't take place until you set the itemssource/datacontext of the ComboBox in the code-behind.

    While you can do it all in code, as you're finding out it's pretty complex and really not the way Silverlight was designed to be used. Is this a strict requirement?

    <TextBlock Text="{Binding MyTextPropertyName}" />

    Sub BindStuff()
    myComboBox.ItemsSource = myDataSource
    End Sub

  • santrajas

    @spivonious :

    Thanks for your reply.

    Yes its kind of strict requirement, because the properties which will be bound with Controls sometimes come from DataBase, As being a developer I know those properties before hand but the way my project is supposed to work, client side (XAML Page) should not have any kind of binding set, all the bindings should be set from Server side not even from .cs file.

    I have tried using Binding object, Loaded Event and few more.

    I have also tried to obtain the TextBlock using LoadContent() function but still somehow binding doesn't take place.

    I hope I made myself clear for your understanding. If you want some more details please let me know.


    Thanks Smiley 


  • spivonious

    @santrajas: I'm extremely confused with the requirement of no binding in the XAML or in the code-behind. Silverlight is the wrong solution if you can't use any of its features. The user of your Silverlight app will have no access to the XAML or CS files.

  • santrajas

    yes that is the exact requirement, user should not have any control over binding,

    user will see data set by server.

    No binding in xaml and no binding in Cs either, server will decide binding and server will set the data using binding.

    I have a control-mapping feature, which maps and stores control and its related datasource in database table.

    eg. TextBox: txtPatientName will be mapped with PatientName property of datasource and this data will be stored in database.

  • philjay

    So in this case, the user HAS control over the databinding? At least the database admin. Is it a requirement, that controls can dynamically change bindings to different column names, even though the properties seem clearly specified(PatientName will always be PatientName and nothing else)?

    A silverlight application gets packaged down to .XAP, and I think the XAML files get compiled as resources into the dll, so a user can't control the bindings anyways.

    Are you sure this is the right approach? Is the database schema given and you basically have to conform to it? You could do it, but I absolutely see no reason to do it(Unless you would want to create something like dynamic formulars, where controls could be dynamically added or removed based on database).

  • santrajas

    @philjay: I dont want to give user access to any kind of data binding hence i want to do binding from server using values and schema stored in database.

    Once the value in DataBase is changed by developer or database admin, the binding of that control should get changed and display the changed value.

    Like Patient Name will always display Patient Name but source from where that values comes can change.

    I am designing a Client-Server Application where client will have Silverlight app running and hence i want to have complete control over binding and values to be displayed this will enable me to make small small changes quite easily. Its a feature i want to add to my project to make it different from other students but seems its bit difficult.

    Can you suggest some way to solve my problem?

  • philjay

    Like when you say, PatientName will always be PatientName, your approach makes no sense, why would you store bindings in a database? I have never seen this before. You store the data in the database, access it using a webservice, then the client application's job is to display this data.

    If you'd store binding-declarations in the db, you could very easily break your application, without any additional gain, but only problems and difficulities, starting at you essentially giving up all the tooling and debugging related to bindings, not even having any additional flexibility, since bindings cannot really change.

    One approach, that if the source can change, eg. different table, I recommend creating a ViewModel representing for example the patient, that has always the same properties, and when loading your data you can just map from whichever table to the properties of that viewmodel. And then you just bind to it.

    But I would not store specific mappings in the db, you should keep the schema clean and consistent, so if you have different tables being displayed by the same view, they should have the same column names. If you need to be really dynamic, you could theoretically store the whole XAML files in the db or the formulars and their fields that will generate a XAML.

  • spivonious

    @santrajas: The client will have zero access to the XAML, so moving the binding code to the server has no benefit. You will end up with a DLL or EXE, not a collection of source files like an ASP.NET site would have.

  • santrajas

    Yeah you are right @spivonious but the thing is I want to add facility to change the displayed data by changing mapping in database.

    For example,

    Combobox which displays names of Doctors who work in clinic, now I want to show list of doctors which are visiting doctors in same combobox instead of previous list.(plz assume labels and other things are in perfect sync to do this). For this thing I need to change column of Datasource Bound with this combobox. 

    I want to achieve this by changing binding through server using data stored in database.

    I hope I am making it little more clear about my scenario.


  • spivonious

    @santrajas: It is starting to make a little more sense, but I'd still try to use code instead of the database.

    For your example, your data access layer could return a doctor list based on given filter criteria. When your user checks a box in the UI, the viewmodel class detects this and modifies the Doctors property to return the different doctors. Your XAML binding doesn't need to change because your ViewModel is now delivering a different list of doctors.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.