Can I indicate which columns can be edited in WPF DataGrid?

I have a DataGrid WPF 4.0 with auto-generated columns. I would only like to allow the user to edit the first column. Is there an easy way to do this?

I tried to add a DataGridCell style and set its editing ability based on either ColumnName (the 1st column always has the same name) or ColumnIndex, however I can not determine the correct XAML for this or even if it is possible.

+6
wpf datagrid editing
source share
4 answers

I got it ... here is what I used:

<DataGrid.Resources> <Style TargetType="{x:Type DataGridCell}"> <Setter Property="IsEnabled" Value="False" /> <Style.Triggers> <DataTrigger Value="PART_IsSelected" Binding="{Binding Path=Column.Header, RelativeSource={RelativeSource Self}}"> <Setter Property="IsEnabled" Value="True" /> </DataTrigger> </Style.Triggers> </Style> </DataGrid.Resources> 

If you want, you can use Column.DisplayIndex instead of Column.Header

I'm still not sure why the binding will not work directly and should reference the RelativeSource, but at least it works :)

+4
source share

The example below uses a trick for one or more columns

  private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) { if (e.Column.Header.ToString() == "COLUMNNAME") { // e.Cancel = true; // For not to include // e.Column.IsReadOnly = true; // Makes the column as read only } } 
+11
source share

Each column has the IsReadOnly property. In addition, the entire DataGrid also has IsReadOnly [This does NOT affect the binding, just the user's ability to edit the field]

EDIT: Sorry, sorry. I can only assume that you should not automatically generate columns, if possible, so you can control which ones are readonly and which controltemplate goes there ... just use the Binding property of the columns, so you do not need to auto-generate them.

+6
source share
  private void dgTableDetailAdj_RowEditEnding (object sender, DataGridRowEditEndingEventArgs e)
 {
     foreach (DataGridColumn col in dgTableDetailAdj.Columns)
     {
         if (col.Header.Equals ("columnName"))
         {
             col.IsReadOnly = true;
         }
     }
 } 
0
source share

All Articles