This is a migrated thread and some comments may be shown as answers.

SetBinding in AutoGeneratingField

2 Answers 85 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Roar
Top achievements
Rank 1
Roar asked on 04 Nov 2015, 10:02 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Martin
Telerik team
answered on 06 Nov 2015, 03:04 PM
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
0
Roar
Top achievements
Rank 1
answered on 10 Nov 2015, 12:41 PM

Thanks, your suggestion worked just perfect :-)

 

Roar

Tags
DataForm
Asked by
Roar
Top achievements
Rank 1
Answers by
Martin
Telerik team
Roar
Top achievements
Rank 1
Share this question
or