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

Cell + Column.IsReadOnlyBinding

6 Answers 164 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alexander
Top achievements
Rank 1
Alexander asked on 03 Mar 2015, 08:48 AM
Hello,

Is there a way to detect if a cell is set readonly by the IsReadOnlyBinding property of the parent column?

I need it for two reasons:

1. I want do display text inside _all_ readonly cells in Italics. The preferred way to do this would be some Trigger in the ControlTemplate of the GridViewCell (I have created my own cell template anyway).

2. I have to check it before entering edit mode. I manually set the IsInEditMode property of the cell on MouseUp, not on MouseDown (as default), as MouseDown should only select the cell.

Alex

6 Answers, 1 is accepted

Sort by
0
Ivan Ivanov
Telerik team
answered on 03 Mar 2015, 09:40 AM
Hello,

Well, this is not supported out-of-the-box, but I believe that it can be achieved with some slight customizations. Can you please confirm whether in your case IsReadOnlyBinding's Source is the underlying data item (the original cell's DataContext) and its Path is a property of this item (like in the following article)?

Regards,
Ivan Ivanov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Alexander
Top achievements
Rank 1
answered on 03 Mar 2015, 09:50 AM
Thanks for your answer. In all our use cases that I am aware of, this is the case. It is a property of the cell datacontext. However, the property varies.
Regarding the IsReadOnlyBinding of the GridView: I forgot take it into account up to now - thanks for reminding (I will have to take it into accout in some spots, for the case that it will be used at some time.)
0
Alexander
Top achievements
Rank 1
answered on 03 Mar 2015, 01:39 PM
I have now created an attached property for the GridViewCell, which is updated by a multibinding (one binding to each IsReadOnly(Binding) property) which is initialized during the Cell.Loaded event:

public static bool GetEditable(DependencyObject obj)
{
    return (bool)obj.GetValue(EditableProperty);
}
 
public static void SetEditable(DependencyObject obj, bool value)
{
    obj.SetValue(EditableProperty, value);
}
 
public static readonly DependencyProperty EditableProperty =
    DependencyProperty.RegisterAttached("Editable", typeof(bool), typeof(Attached), new PropertyMetadata(true));

public static MultiBinding DefaultReadOnlyBinding = CreateReadOnlyBinding();
 
private static MultiBinding CreateReadOnlyBinding()
{
    var multiBinding = new MultiBinding { Converter = BoolNoneConverter.Default };
 
    multiBinding.Bindings.Add(new Binding("Column.IsReadOnly") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });
    multiBinding.Bindings.Add(new Binding("Column.Parent.IsReadOnly") { RelativeSource = new RelativeSource(RelativeSourceMode.Self) });
    return multiBinding;
}
 
private void Cell_Loaded(object sender, RoutedEventArgs e)
{
    var cell = (GridViewCell)sender;
    var grid = cell.Column.Parent as RadGridView;
    var column = cell.Column as GridViewBoundColumnBase;
 
    var multiBinding = DefaultReadOnlyBinding;
 
    if ((grid != null && grid.IsReadOnlyBinding != null) || (column != null && column.IsReadOnlyBinding != null))
    {
        multiBinding = CreateReadOnlyBinding();
        if (grid != null && grid.IsReadOnlyBinding != null) multiBinding.Bindings.Add(grid.IsReadOnlyBinding);
        if (column != null && column.IsReadOnlyBinding != null) multiBinding.Bindings.Add(column.IsReadOnlyBinding);
    }
 
    cell.SetBinding(Attached.EditableProperty, multiBinding);
}

This can be easily accessed from a trigger.

Alex
0
Ivan Ivanov
Telerik team
answered on 03 Mar 2015, 02:04 PM
Hi,

I am glad to hear that you have found a solution. Thank you for posting it to be available for the rest of the community.
As a side note, if you have a lot of items/properties, CellLoaded will be raised quite often while scrolling (I presume that you have UI virtualization turned on). If setting up and evaluating a multibinding turns out to be slow, you can consider using some caching of the Editable values in your ViewModel. Thus you would be able to just look the values up, instead of reevaluating them. 

Regards,
Ivan Ivanov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Alexander
Top achievements
Rank 1
answered on 03 Mar 2015, 02:22 PM
I don't see any way how caching could be possible as the cell template is used for all grids with different kinds of viewmodels.

The only question could be if there is any faster alternative than the multibinding. E.g. the grid and/or the column could be responsible to update the corresponding cells (especially if there is no IsReadOnlyBinding set).
0
Ivan Ivanov
Telerik team
answered on 06 Mar 2015, 12:22 PM
Hello,

I believe that this is possible, but it would need a bit more complex set up. You can try using an attached behavior that is attached to a RadGridView instance. On the PropertyChanged callback of the attached property, you can subscribe to CellLoaded. On the event occurrance, you can either evaluate the value that you need, with a binding or with another member access approach, or take it from the ViewModel cache. You can access the ViewModel in the event handler as it would be RadGridView's (sender) DataContext. Finally, you can set the retrieved value to the second attached property and similarly to your approach define a trigger condition for it.

If you seek for a faster alternative to data-binding, you can try using the .net Expression api to retrieve a property value.

Regards,
Ivan Ivanov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
Alexander
Top achievements
Rank 1
Answers by
Ivan Ivanov
Telerik team
Alexander
Top achievements
Rank 1
Share this question
or