SetBinding in AutoGeneratingField

3 posts, 1 answers
  1. Roar
    Roar avatar
    9 posts
    Member since:
    Jul 2012

    Posted 04 Nov 2015 Link to this post

    Hi,

    I have a DataForm with AutoGenerateFields="True". In the AutoGeneratingField event handler I want to set a binding on the IsReadOnly property for some of the fields, so that when the FromProfile property is checked, those fields will be set as ReadOnly. The strange thing is that the binding only works if FromProfile is True when the AutoGeneratingField is fired. The DataField's ("Port", "Protocol" and "Interface") is set ReadOnly whenever the "FromProfile" DataField is checked. If FromProfile is False at the time the binding is set up, it doesn't work.

    The AutoGeneratingField event handler looks like this:

    01.if (e.DataField.Label.Equals("Port") || e.DataField.Label.Equals("Protocol") || e.DataField.Label.Equals("Interface"))
    02.{
    03.    var binding = new Binding("FromProfile")
    04.    {
    05.        Mode = BindingMode.OneWay,
    06.        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
    07.    };
    08.    e.DataField.SetBinding(DataFormDataField.IsReadOnlyProperty, binding);
    09.}
     

    Any idea why it doesn't work?

     

    Regards,

  2. Answer
    Martin Vatev
    Admin
    Martin Vatev avatar
    87 posts

    Posted 06 Nov 2015 Link to this post

    Hi Roar,

    This behavior is caused by the fact that we internally change the IsReadOnly property of the field when you begin to edit, thus the binding is cleaned. I would recommend two approaches to achieve the desired behavior. The first one is to set the AutoEdit property to "True". The second one is to set the binding when BeginEdit event is raised. Please take a look at the code snippets below:

    private void DataForm1_BeginningEdit(object sender, System.ComponentModel.CancelEventArgs e)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
        {
            foreach (DataFormDataField dataField in this.DataForm1.ChildrenOfType<DataFormDataField>())
            {
                if (dataField.Label.Equals("Salary") || dataField.Label.Equals("Occupation") || dataField.Label.Equals("FirstName"))
                {
     
                    var binding = new Binding("IsMarried")
                    {
                        Mode = BindingMode.OneWay,
                        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                    };
                    dataField.SetBinding(DataFormDataField.IsReadOnlyProperty, binding);
                }
            }
        }));
    }

    I hope this helps.

    Regards,
    Martin Vatev
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WPF is Visual Studio 2017 Ready
  4. Roar
    Roar avatar
    9 posts
    Member since:
    Jul 2012

    Posted 10 Nov 2015 in reply to Martin Vatev Link to this post

    Thanks, your suggestion worked just perfect :-)

     

    Roar

Back to Top