I added IsRowReadOnly and IsCellReadOnly bindable properties (link) to Microsofts DataGrid but I would really like to take advantage of 1) the fact that we own Telerik controls, and 2) the features RadGridView offers. I do NOT want to have to customize the source every time you release. I do NOT want to fiddle with styles to fake it.
How can this be accomplished?
23 Answers, 1 is accepted
We will consider adding such properties, but I cannot commit with any specific date or release when these properties will be introduced.
As a workaround I could suggest you to use RadGridView.BeginningEdit event which can cancel the entire edit process. Let me know if this doesn't help.
Best wishes,
Nedyalko Nikolov
the Telerik team
I most recently played with a custom column that does accomplish (in part) what we need.
public class GridViewDataColumn : GridViewBoundColumnBase { /// <summary> /// To allow for defining the binding in XAML /// </summary> public Binding IsCellReadOnlyBinding { get; set; } #region Attached Property /// <summary> /// Bound property to attach to the cell /// </summary> public static readonly DependencyProperty IsCellReadOnlyProperty = DependencyProperty.RegisterAttached( "IsCellReadOnly", typeof(bool), typeof(GridViewDataColumn), null); public static void SetIsCellReadOnly(DependencyObject element, bool value) { element.SetValue(IsCellReadOnlyProperty, value); } public static bool GetIsCellReadOnly(DependencyObject element) { return (bool)element.GetValue(IsCellReadOnlyProperty); } #endregion public override FrameworkElement CreateCellEditElement(Telerik.Windows.Controls.GridView.GridViewCell cell, object dataItem) { //attach our property to the cell and set the binding //since the binding has no "Source" it will use the datacontext of the cell cell.SetBinding(GridViewDataColumn.IsCellReadOnlyProperty, IsCellReadOnlyBinding); var row = dataItem as ViewModel; if (row == null) throw new Exception("Png.GridViewDataColumn requires a bound item of base type ViewModel"); if (row.IsReadOnly || (bool)(cell.GetValue(GridViewDataColumn.IsCellReadOnlyProperty))) { cell.IsInEditMode = false; return base.CreateCellElement(cell, dataItem); } return base.CreateCellEditElement(cell, dataItem); } } So in XAML
<com:GridViewDataColumn Header="Begin Date" IsCellReadOnlyBinding="{Binding EntityBeginDate.IsReadOnly}" Width="100" IsSortable="True" MinWidth="50" DataMemberBinding="{Binding EntityBeginDate.MappedValue, Converter={StaticResource DateConverter}}"> <com:GridViewDataColumn.CellTemplate> <DataTemplate> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding EntityBeginDate.MappedValue, Converter={StaticResource DateConverter}}" Margin="4,0,4,0"/> </DataTemplate> </com:GridViewDataColumn.CellTemplate> <com:GridViewDataColumn.CellEditTemplate> <DataTemplate> <controls:DatePicker SelectedDate="{Binding EntityBeginDate.MappedValue, Converter={StaticResource DateConverter}, Mode=TwoWay}"/> </DataTemplate> </com:GridViewDataColumn.CellEditTemplate> </com:GridViewDataColumn>Also by moving the attached property to CreateCellElement you can check the attachedproperties value in BeginningEdit in which case you can cancel the edit but it (CreateCellElement) fires a lot more then may be needed and if I have to add custom code I prefer to keep in in one place rather then splitting them to custom column and code behind.
Any other ideas?
Indeed you are right with Template selector feature you will need two templates.
Unfortunately with the current version of the grid I cannot suggest you a better solution than using RadGridView.BeginningEdit event. As I said we will consider adding such functionality to RadGridView control.
Regards,
Nedyalko Nikolov
the Telerik team
My current solution, which is working fine, is to create a custom column for each telerik column (using the corresponding telerik column as its base) and add an IsCellReadOnlyBinding property to the column. Then in the Beginning edit I pull this Binding and bind it to an attached property of GridViewCell and then GetValue on that attached property to determine if I should cancel. The only problem is it wont respond to any changes to that bound property but I am ok with this limitation.
I look forward to a built in mechanism :)
Thank you for your valuable feedback. I hope that we will be able to introduce the mentioned improvements as soon as possible.
Sincerely yours,
Milan
the Telerik team
Take a look at the attached example. It demonstrates a new property called IsReadOnlyBinding which can be set on a column or on a grid level.
The example demonstrates how second row ("Doe 1") is read only except the "Age" cell (which has its own IsReadOnlyBinding).
Let me know how example works on your end.
Greetings,
Nedyalko Nikolov
the Telerik team
1) I expected that row readonly would override the cell. Meaning if I want to stop the whole row from being edited I would not expect a cell's readonly property to take precedence. My personal implementation only checks the cell if the row is editable.
In our application each "property" which is editable has additional properties that regulate whether its readonly and its validation. Then again the the row level we have a readonly property. So either through UI or code behind if you try to set one of those properties it checks if the row is editable, if so it checks if that property is editable, if so it edits and performs validation.
2) This isn't as important as #1. If a cell is in edit mode, and the IsReadOnlyBinding property changes I would expect the cell to cancel its edit and leave edit mode. Likewise with the row level, if at row level goes readonly and the row is being edited, cancel the edit and return to view mode. This is how I had the microsoft grid working but it rarely was used. The only case we had in which this was triggered was if security permissions changed in mid-edit which is not common.
It is very nice to see your progress so quickly.
Straight onto your questions (thoughts):
1. You expect that row should take precedence over cell value, but I cannot agree with that. Anywhere in xaml technologies the closer setter takes precedence. For example if you set TextBlock.Background and its parent Border.Background which background will be applied on the textblock?
2. We will consider adding almost same functionality. By almost same I mean that we could add check to IsReadOnlyBinding value on Cell.CommitEdit procedure and take the appropriate actions there.
Thank you for valuable feedback.
Regards,
Nedyalko Nikolov
the Telerik team
If we have a Row.IsReadOnly that has no control over the cells (if they also define isreadonlybinding) then it is argueably almost useless when we could simply bind each column.isreadonlybinding to the same value as the row.isreadonly - you have simply reduced the number of definitions needed. By allowing for an order of precedence I believe it offers the user more functionality then simply reducing the number of definitions.
We reconsidered our vision about "IsReadOnlyBinding property" precedence. You can download our latest internal build and check how it goes on your end along with my apologies for the inaccurate comparison.
Thank you for your valuable feedback.
Best wishes,
Nedyalko Nikolov
the Telerik team
So I tried your latest build and it has worked so well that I have removed all my changes and have incorporated your latest build in our production bound build. It has passed our internal tests and allowed us to simplify our templates now that the features are built in! Thank you very much and I look forward to your next full release!
I'm glad that everything is working fine on your end.
The official release (a.k.a 2010.Q2.SP2) will be released in a week or two.
Sincerely yours,
Nedyalko Nikolov
the Telerik team
You can download service pack 2 (2010.Q2.SP2) from your Telerik account (IsReadOnlyBinding property is included).
Best wishes,
Nedyalko Nikolov
the Telerik team
xmlns:local="clr-namespace:IsReadOnlyBindingExample"
I added this to my xaml and it isn't found. Please advise. I am using v.2010.2.924.1040 Trial
If you do not have such namespace in your project you cannot expect this to work.
Regards,Vlad
the Telerik team
This is namespace declaration to the local application namespace. In case of our example this was IsReadOnlyBindingExample. When you have such declaration in your XAML you will be able to declare classes from this namespace.
Regards,Vlad
the Telerik team
I've just tested the example with 2010.Q2.SP2 binaries and everything works fine on my end. According to xmlns:local="clr-namespace:IsReadOnlyBindingExample" namespace declaration this is just a reference to the example project which uses a custom value converter. In order to use IsReadOnlyBinding feature of the RadGridView you don't need this namespace declaration.
I hope this makes sense.
Nedyalko Nikolov
the Telerik team
All is fine now - I was trying to follow the example, but in fact I found I just needed to put IsReadOnlyBinding on the GridViewDataColumn definitions. No need for the readonly converter etc.
