There is a grid

<DataGrid ItemsSource="{Binding Path=MyObservableCollection, Mode=OneWay}" SelectionMode="Extended" IsReadOnly="True" CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" SelectedItem="{Binding Path=SelectedValue, Mode=OneWayToSource}"> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=OneWay}" Width="40*"/> <DataGridTextColumn Header="Type" Binding="{Binding TypeText, Mode=OneWay}" Width="10*" /> </DataGrid.Columns> <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Setter Property="IsHitTestVisible" Value="true или false"/> </Style> </DataGrid.RowStyle> </DataGrid> 

I want to, depending on the ReadOnly property in the MyObservableCollection collection, the rows become non-separable, the remaining rows can be selected. Is it possible to do this and how?

Collection type:

 public class MyType { public string Name {get; set;} public string TypeText {get; set;} public bool ReadOnly {get; set;} } 

    1 answer 1

    I did it with a converter

      <DataGrid.RowStyle> <Style TargetType="DataGridRow"> <Style.Triggers> <DataTrigger Binding="{Binding Path=field, Converter={StaticResource UnselectRowConverter}}" Value="True"> <Setter Property="IsEnabled" Value="False" /> <Setter Property="Foreground" Value="LightGray" /> </DataTrigger> </Style.Triggers> </Style> </DataGrid.RowStyle> 

    It remains only to implement the logic of locking rows

    • The point is not the point is to bind the property MyType.ReadOnly to binding. And I do not know how to do it. Because Binding doesn't see him - user2455111
    • <DataGridTextColumn Header = "ReadOnly" Binding = "{Binding ReadOnly, Mode = OneWay}" Width = "10 *" />, if you have this property in ViewModel, then make it bool and hang the converter. There should be no problems with binding. - Valery Gorbunov
    • It is not there, but it is in the collection, I try to pull it out through {Binding RelativeSource = {RelativeSource FindAncestor, AncestorType = {x: Type DataGridRow}}, Path = Item, Converter = {StaticResource AssemblyTypeToBoolConverter}} but so far without success - user2455111
    • Why converter? The author of the question for the lock is responsible property ReadOnly . Those. just write this: gist.github.com/FoggyFinder/13a0b00b9a411f78164dcf68e22171d7 - user227049
    • one
      @ user2455111 You are simply applying the wrong style - user227049