【问题标题】:How can i use DatagridTextColumn.ElementStyle to Make DatagridTextColumn readonly?如何使用 DatagridTextColumn.ElementStyle 将 DatagridTextColumn 设为只读?
【发布时间】:2012-08-01 08:51:24
【问题描述】:

我可以使用 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>

【问题讨论】:

  • 确定我做到了!!它没有绑定!!
  • IsReadOnlyProperty 不在 datagrid Itemsource 中,它被放置在 ViewModel 中。
  • @raha:您的 ItemsSource 应该是视图模型的集合,那么在哪个上下文中可以访问该视图模型?
  • 但它有效!!视图模型在窗口上下文中!

标签: wpf wpfdatagrid datagridtextcolumn


【解决方案1】:

试试这个:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    相关资源
    最近更新 更多