-1

I'm binding a list of objects to a DataGrid programatically as follows:

gridAssetParameters.ItemsSource = asset.Parameters;

Parameters is a simple class with three properties: Name, Value, Description and Editable. The columns generate automatically when setting the source to asset.Parameters, however the problem that I'm facing is I cannot make a row readonly based on the property Editable. I was thinking of using the below event to manually make the rows readonly, however there is no way of looping through the generated rows or settings rows as readonly.

private void GridAssetParameters_AutoGeneratedColumns(object sender, EventArgs e)
{
   //Somehow loop through rows and enable/disable based on logic
}

I am aware of MVVM however am unsure of how to use it in this case. Any advice will be appreciated!

UPDATE

Answer to my question was found here:

https://stackoverflow.com/a/17216605/1035217

ceds
  • 2,097
  • 5
  • 32
  • 50
  • Unless you really have to, it's best to avoid auto generating columns for a datagrid. Or editing directly in the datagrid. I would usually make one readonly and edit in a panel overlaying the datagrid. This way there's somewhere to do validation and it's easy to ensure the user explicitly commits or abandons edits. And you could then disable the "edit" button when the selecteditem's ieseditable is false. – Andy Feb 05 '20 at 13:59

1 Answers1

0

If you want to create columns in XAML

<DataGrid ItemsSource="{Binding Path=Parameters}" 
          AutoGenerateColumns="False" 
          IsReadOnly="True" 
          Name="ResultGrid"> 
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Setter Property="IsEnabled" Value="{Binding Editable}"/>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns> 
        <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="Auto"/> 
        <DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" Width="*"/> 
        <DataGridTextColumn Header="Description" Binding="{Binding Path=Description}" Width="*"/> 
    </DataGrid.Columns> 
</DataGrid> 

If you want to auto generate columns

<DataGrid ItemsSource="{Binding Path=Parameters}" 
          AutoGenerateColumns="True" 
          IsReadOnly="True" 
          Name="ResultGrid"> 
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Setter Property="IsEnabled" Value="{Binding Editable}"/>
        </Style>
    </DataGrid.Resources>     
</DataGrid> 
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • Using this I cannot AutoGenerate columns? I'm still trying to get my head around data templates and styles. Very much still stuck in a WinForms line of thinking – ceds Feb 05 '20 at 13:16
  • 1
    @ceds: You can if you want, have a look at the updated answer. – CharithJ Feb 05 '20 at 13:28
  • Settings IsReadOnly = true makes all rows readonly. Setting it to false is not giving the desired results (all rows are editable). This is more troublesome than I thought... – ceds Feb 05 '20 at 13:41