-1

I have a DataGrid (below) that takes in SimulatedItems as an ItemsSource which is from my viewmodel. The SimulatedItems is an ObservableCollection of SimulatedItem. Inside SimulatedItem there is a variable DriverID, NameID and Active. Both the DataGridTextColumns below correctly reference these variables inside for Binding but for IsReadOnly it does not work. It appears IsReadOnly is attempting to bind to my viewmodel rather than the SimulatedItems. How do I get IsReadOnly to bind to my item from ItemsSource?

<DataGrid Grid.Row="1" ItemsSource="{Binding SimulatedItems}">
  <DataGrid.Columns>
    <DataGridTextColumn
      Header="NameID"
      Binding="{Binding NameID}"
      IsReadOnly="{Binding Active}">
    </DataGridTextColumn>
    <DataGridTextColumn
      Header="DriverID"
      Binding="{Binding Path=DriverID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
      IsReadOnly="{Binding Active}">
    </DataGridTextColumn>
  </DataGrid.Columns>
</DataGrid>
Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
bigboy
  • 35
  • 5
  • What is the datatype of the Active property. Is it a string or a boolean. – Scott Brady Feb 10 '22 at 04:37
  • It is a boolean – bigboy Feb 10 '22 at 04:44
  • If i read your question correctly then this article on stackoverflow may help. https://stackoverflow.com/questions/3222856/datagridtextcolumn-isreadonly-seems-to-be-faulty – Scott Brady Feb 10 '22 at 05:03
  • This solution still causes the cell to look at my viewmodel rather than my SimulatedItem from SimulatedItems – bigboy Feb 10 '22 at 05:54
  • How is the columns supposed to bind to a property of an individual item? Do you want to enable or disable the whole column? – mm8 Feb 10 '22 at 15:22

1 Answers1

0

Unfortunately this will not work.

DataContexts do not work with DataGridColumns. The binding can't find the datacontext.

Read more about it here https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

Solution: Use templates

<DataGrid Grid.Row="1" ItemsSource="{Binding SimulatedItems}">
     <DataGrid.Columns>
           <DataGridTemplateColumn Header="NameID" Width="200">
                 <DataGridTemplateColumn.CellTemplate>
                      <DataTemplate>
                          <TextBox Text="{Binding NameID}" IsReadOnly="{Binding  Active}" BorderThickness="0" Background="Transparent"/>
                      </DataTemplate>
                 </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="DriverID" Width="200">
                  <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <TextBox Text="{Binding Path=DriverID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="{Binding Active}" BorderThickness="0" Background="Transparent"/>
                         </DataTemplate>
                  </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
     </DataGrid.Columns>
</DataGrid>
enesimam
  • 106
  • 3