I have a situation that needs to conditionally make readonly to wpf datagrid cell. There is IsReadOnly property in DataGridCell. But unfortunately, that property is readonly! Is there any way to do it?
ant.
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
You should be able to use the DataGrid.BeginningEdit event to conditionally check if the cell is editable and then set the Cancel property on the event args if not.
The similar solution as Goblin above, but with a little code samples:
The idea is to dynamically switch the
CellEditingTemplate
between two templates, one is the same as the one in theCellTemplate
, the other is for editing. This makes the edit mode acts exactly the same as the non-editing cell although it is in edit mode.The following is some sample code for doing this, notice that this approach requires
DataGridTemplateColumn
:First, define two templates for read-only and editing cells:
Then define a data template with additional
ContentPresenter
layer and useTrigger
to switch theContentTemplate
of theContentPresenter
, so the above two templates can be switched dynamically by theIsEditable
binding:HTH
Another very simple solution to this problem is to use a Style of the DataGridCell
This style assumes that there is a IsEnabled property in the ViewModel.
This does not make the cell read only but disabled. It is almost the same thing except that is cannot be selected. This solution might not be applicable in all cases due to this.
You could also use the TemplateSelector property to set two different DataTemplates (one writable and one readonly) based on your logic? Just create a class that inherits from DataTemplateSelector and override the SelectTemplate() method (here you have access to the datacontext).