简体   繁体   中英

How can i use DatagridTextColumn.ElementStyle to Make DatagridTextColumn readonly?

can i use elementStyle to bind DatagridTextColumn IsReadOnly to a Property inside a ViewModel(here IsReadOnlyProperty)? something like this:

  <DataGridTextColumn.ElementStyle>
      <Style TargetType="{x:Type TextBlock}">
          <Setter Property="IsEnabled" Value="{Binding IsReadOnlyProperty}"/>
      </Style>
  </DataGridTextColumn.ElementStyle>
  <DataGridTextColumn.EditingElementStyle>
        <Style TargetType="{x:Type TextBlock}">
               <Setter Property="IsEnabled" Value="{Binding IsReadOnlyProperty}" />
        </Style>
  </DataGridTextColumn.EditingElementStyle>

Try this:

<DataGridTextColumn IsReadOnly="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.IsReadOnlyProperty}" />

When you create DataGrid, you set its DataContext by assigning ItemsSource. This limits ALL controls inside DataGrid to only use objects from their parent DataContext, so if you want to bind to a property from outside of this DataContext, you have to explicitly declare that you want to change your scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM