0

我可以使用 elementStyle 将 DatagridTextColumn IsReadOnly 绑定到 ViewModel 内的属性(此处为 IsReadOnlyProperty)吗?像这样的东西:

  <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>
4

1 回答 1

1

尝试这个:

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

创建 DataGrid 时,通过分配 ItemsSource 设置其 DataContext。这将 DataGrid 内的所有控件限制为仅使用其父 DataContext 中的对象,因此如果您想绑定到此 DataContext 外部的属性,您必须明确声明您要更改您的范围。

于 2017-09-05T13:29:43.690 回答