Accessing DataTemplate fields

3 posts, 0 answers
  1. Mrn
    Mrn avatar
    29 posts
    Member since:
    Apr 2011

    Posted 25 Apr 2011 Link to this post


    I have several controls such as radio buttons within a DataTemplate I set as the EditTemplate of a RadDataForm as shown in the "Customized Fields" page code snippet. I'm trying to access the controls of that DataTemplate in code-behind in a similar way as it is supposed to be possible in a SL toolkit DataForm control:

    public MainPage() 
                Loaded += MainPage_Loaded; 
                DataForm1.Loaded += new RoutedEventHandler(DataForm1_Loaded);            
    void DataForm1_Loaded(object sender, RoutedEventArgs e) 
               currentRadioButton = ((RadDataForm)sender).FindName("radioButtonCurrent") as RadioButton;             
               currentRadioButton.FontWeight = FontWeights.Bold; 

    I also tried by accessing the 'Resources' member:

    currentRadioButton = ((RadDataForm)sender).Resources["radioButtonCurrent"] as RadioButton;

    but when the line where the font weight is changed is reached, I get an exception because currentRadioButton is always null. Is there any way to access the controls within a DataTemplate in code-behind to change dynamically their properties?

  2. Pavel Pavlov
    Pavel Pavlov avatar
    2039 posts

    Posted 25 Apr 2011 Link to this post

    Hello Mrn,

    You can subscribe to the Loaded event of a certain control inline the XAML template.
    Once the control is loaded , you can manipulate it by code inside the event handler.

    Can you elaborate a bit more what is the final goal ? Maybe we can find a better way. Also we are interested in improving our API to cover your needs , so any details would be highly appreciated .

    Best wishes,
    Pavel Pavlov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Mrn
    Mrn avatar
    29 posts
    Member since:
    Apr 2011

    Posted 25 Apr 2011 Link to this post

    Ok, I'm going to explain in more detail what I'm trying to do:

    I defined a group of RadioButtons together with other controls (RadNumericUpDown, DataFormDateField ones and some others) within the mentioned DataTemplate that is binded as a static resource to the RadDataForm control. I want that group of radio buttons to act as a selector of, let's say, the type of the current item bound to the RadDataForm (they're just bound to an enum-type field of the object, not to different type objects), and, according to the business logic of my app, when selecting each of the radio buttons, different fields of the rest of the data form become mandatory (being the rest optional). What I'd like to do is to bind those radio buttons to the rest to the fields in such a way that, when the user selects a certain radio button, the labels (or associated textblocks) of the fields I want to become mandatory are displayed in a different font weight (i.e. bold) in order to make the user be aware of what fields are then required before submitting the form. That's why I'm first trying to reference controls within a data template in code-behind.

    I hope I have explained myself...
Back to Top