EnsureTwoWay

Aug 26, 2008 at 5:28 PM
In the DataGrid, does anyone know why the DataGridBoundColumn property Setter for the DataFieldBinding Property calls the "EnsureTwoWay"? This bascially eliminates the possibility to data bind to a read only property.

Does anyone know what assumptions are built around this? If I just remove the call to "EnsureTwoWay" what else might break?


Coordinator
Aug 26, 2008 at 6:46 PM
This is mainly for cell editing to work properly.  General row editing will use the IEditableObject pattern for transactional editing but cell editing uses a custom implementation in the DataGridBoundColumn class.  Here is what you can do for read-only properties to work (which is a snippet from DataGridColumn.cs when columns are auto-generated):  Notice that you have to do this after setting boundColumn.DataFieldBinding since it is in that setter where EnsureTwoWay is applied.

//Set the data field binding for such created columns and

 

//choose the BindingMode based on editability of the property.
DataGridBoundColumn boundColumn = dataGridColumn as DataGridBoundColumn;
if (boundColumn != null)
{
    Binding binding = new Binding(itemProperty.Name);
    boundColumn.DataFieldBinding = binding;
    PropertyDescriptor pd = itemProperty.Descriptor as PropertyDescriptor;
    if (pd != null)
    {
        if (pd.IsReadOnly)
        {
            binding.Mode =
BindingMode.OneWay;
            boundColumn.IsReadOnly =
true;
        }
    }
    else
    {
        PropertyInfo pi = itemProperty.Descriptor as PropertyInfo;
        if (pi != null)
        {
            if (!pi.CanWrite)
            {
                binding.Mode =
BindingMode.OneWay;
                boundColumn.IsReadOnly =
true;
            }
        }
    }
}


 

Aug 26, 2008 at 7:03 PM
Edited Aug 26, 2008 at 7:43 PM
I got it working with this technique in my procedural code but:

1) It looks like the current released code has "IsReadOnly" on DataGridColumn set as "internal" which means I either compile the toolkit into my own exec or I modify the toolkit source (which is what I did).

2) This must be done through procedural code (not XAML) correct?

My object structures are pretty lengthy/complex so would I be better off auto generating and in the autogeneration event and only including the columns I want?
Coordinator
Aug 26, 2008 at 8:09 PM
IsReadOnly will be public in the final release version.

That is correct that it has to be done in procedural code after setting the DataFieldBinding. 

Validating which columns you want in the autogeneration event is a valid solution since you plan to write procedure code for initalizing the columns anyway.  If you have a standard set of columns that will always be displayed you can declare them statically in xaml and then update the read-only ones in code if you want.  There are pro's and con's to each.
Aug 27, 2008 at 1:10 PM
In the final release will we be able to not require the procedural code? Meaning, if we do not AutoGenerateColumns and manually add the mappings and the Bindings are set in XAML as OneWay, will the datagrid observe these settings?
Coordinator
Aug 27, 2008 at 6:50 PM
In the final release this will actually be fixed so you won't need to write the procedural code for it and the TwoWay binding will not happen when you set the DataFieldBinding for a read-only property.
Aug 30, 2008 at 3:30 AM
Well hurry up then!   :)