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

Binding issue when using dynamic fields and programmatically generated custom columns

2 Answers 74 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 28 Mar 2013, 03:24 PM
Here's a link to a sample project that illustrates the problem I'm having: https://dl.dropbox.com/u/109257/RadControlsSilverlightApp1.zip

I'm trying to create a custom column for the RadGridView that contains a TextBlock and RadButton when not in edit mode and a control called LookupControl which contains a TextBox and RadButton, but I am having some issue with the binding, and I'm not sure where I have gone wrong. My grid is populated with dynamic data using the method and classes found here: http://blogs.telerik.com/vladimirenchev/posts/11-09-28/dynamic-binding-for-your-silverlight-applications.aspx

In the sample project you will see in DetailGrid.xaml.cs see: 
private void RadGridView1_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
{
    if (e.Column.UniqueName == "Column4")
    {
        e.Cancel = true;
 
        LookupColumn column = new LookupColumn()
        {
            DataMemberBinding = new Binding("Data[" + e.Column.UniqueName + "]")
            {
                Mode = BindingMode.TwoWay,
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
            },
            Header = e.Column.UniqueName
        };
 
        ((RadGridView)sender).Columns.Add(column);
    }          
}

where for one column I am adding my custom LookupColumn. In the LookupColumn class you will see that when a cell enters edit mode I create an instance of the LookupControl, binding the dataItem to the TextProperty dependency property which binds to LookupControl.MyText. Process shown below:

public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
{
    this.BindingTarget = LookupControl.TextProperty;
    var picker = new LookupControl();
 
    picker.SetBinding(LookupControl.TextProperty, new Binding()
    {
        Mode = BindingMode.TwoWay,
        NotifyOnValidationError = true,
        ValidatesOnExceptions = true,
        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
        Source = dataItem,
        Path = new PropertyPath(this.DataMemberBinding.Path.Path)
    });
    return picker;
}

The MyText property of the LookupControl is bound to the Text property of the child TextBox.

Now that I've layed out how I have things set up, the problem I'm having is that when you run the program and put Column4 in edit mode, type in a value and tab off the control, the value displays properly in non-edit mode. However, if you type a value into the TextBox of Column4 and click off of it the value does not display properly, but if you click back in the cell, and again click off of it it does start displaying the value correctly. It also appears that when the value displays incorrectly the underlying dataItem and collection are not being updated.

I've narrowed down the issue to that fact that for some reason the way I have my binding setup isn't working 100% correctly. In MyDataRow the set portion of the following function

public object this[string columnName]
{
    get
    {
        if (data.ContainsKey(columnName))
        {
            return data[columnName];
        }
 
        return null;
    }
    set
    {
        if (!data.ContainsKey(columnName))
        {
            data.Add(columnName, value);
 
            OnPropertyChanged(columnName);
        }
        else
        {
            if (data[columnName] != value)
            {
                data[columnName] = value;
 
                OnPropertyChanged(columnName);
            }
        }
    }
}

never gets hit in the debugger no matter how you manipulate the data in the custom column, which I guess means the PropertyChanged event never gets fired, which probably has something to do with the inconsistencies I'm seeing. On the otherhand, the non-custom, standard columns always fire the setter, thus firing the PropertyChanged event, and I'm having no problems with them.

Is there something I have configured wrong? Is this not expected functionality? I'm at a loss at the moment and would appreciate any help you can offer. Let me know if I need to clarify anything.

Thanks.


2 Answers, 1 is accepted

Sort by
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 02 Apr 2013, 10:17 AM
Hi,

You should add Binding.UpdateSourceTrigger = PropertyChanged to the TextBox control inside your LookupControl, otherwise TextBox will update source property on LostFocus (default value):

<TextBox x:Name="text" Height="20" Text="{Binding ElementName=root, Path=MyText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="0"/>

All the best,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Andrew
Top achievements
Rank 1
answered on 02 Apr 2013, 11:46 AM
Thanks, that was part of my problem. The other issue was that I didn't need to bind my controls in the LookupColumn.cs class to update on PropertyChanged, as the Text property of the LookupControl.cs handles that part for me now.

Thanks again for your help!
Tags
GridView
Asked by
Andrew
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Andrew
Top achievements
Rank 1
Share this question
or