this.SampleDataGrid.CurrentCellBeginEdit += SampleDataGrid_CurrentCellBeginEdit; private void SampleDataGrid_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs args) { if(args.Column.MappingName=="Value") { var rowIndex = args.RowColumnIndex.RowIndex; var recordIndex = this.SampleDataGrid.ResolveToRecordIndex(rowIndex); var record = this.SampleDataGrid.View.Records[recordIndex].Data as DataItem; if (record.ItemType == 2 && record.ItemSubType == null) args.Cancel = true; } } |
public class CustomStyleSelector:StyleSelector { public override Style SelectStyle(object item, DependencyObject container) { var data = item as DataItem; if(data.ItemType==2 && data.ItemSubType==null) return Application.Current.FindResource("DarkGrayStyle") as Style; else return Application.Current.FindResource("BlueStyle") as Style; return base.SelectStyle(item, container); } } |
Query |
Details |
You realized the requirement of "locked" cells with cancelling edit within the CurrentCellBeginEdit event. But wouldn't it be better to bind AllowEdit property to the datasource? |
It is possible to set AllowEditing for column, but no direct way to set AllowEditing for each cell. If you want to handle editing in cell basics, then handling CurrentCellBeginEdit event is the best way.
In another way, set the IsEnabled property in style as false instead of handling CurrentCellBeginEdit event. So the cell won’t get into edit mode and also it is not possible to select the cell by user.
<Style x:Key="DarkGrayStyle" TargetType="{x:Type sf:GridCell}">
<Setter Property="Background" Value="DarkGray" />
<Setter Property="IsEnabled" Value="False"/>
<Setter Property="Padding" Value="20 0" />
</Style> |
the cell style (BlueStyle for editable and DarkGrayStyle for non editable) is completely not realized in your approach. |
We could not get your query clearly. Style also realized in Style selector. Please refer the code snippet below,
public class CustomStyleSelector:StyleSelector
{
public override Style SelectStyle(object item, DependencyObject container)
{
var data = item as DataItem;
if(data.ItemType==2 && data.ItemSubType==null)
return Application.Current.FindResource("DarkGrayStyle") as Style;
else
return Application.Current.FindResource("BlueStyle") as Style;
return base.SelectStyle(item, container);
}
}
|
this.AssociatedObject.CurrentCellActivating += AssociatedObject_CurrentCellActivating; private void AssociatedObject_CurrentCellActivating(object sender, CurrentCellActivatingEventArgs e) { if (this.AssociatedObject.Columns[e.CurrentRowColumnIndex.ColumnIndex].MappingName == "Value") { var rowIndex = e.CurrentRowColumnIndex.RowIndex; var recordIndex = this.AssociatedObject.ResolveToRecordIndex(rowIndex); var record = this.AssociatedObject.View.Records[recordIndex].Data as DataItem; if (record.ItemType == 2) e.Cancel = true; } } |
private void AssociatedObject_CurrentCellBeginEdit(object sender, CurrentCellBeginEditEventArgs e) { if (this.AssociatedObject.Columns[e.RowColumnIndex.ColumnIndex].MappingName == "Value") { var rowIndex = e.RowColumnIndex.RowIndex; var recordIndex = this.AssociatedObject.ResolveToRecordIndex(rowIndex); var record = this.AssociatedObject.View.Records[recordIndex].Data as DataItem; if (record.ItemType == 2) { e.Cancel = true; } } } |