Wpf 如何使用DatagridTextColumn.ElementStyle使DatagridTextColumn成为只读?
是否可以使用elementStyle将DatagridTextColumn IsReadOnly绑定到ViewModel中的属性(此处为ReadOnlyProperty)? 大概是这样的:Wpf 如何使用DatagridTextColumn.ElementStyle使DatagridTextColumn成为只读?,wpf,wpfdatagrid,datagridtextcolumn,Wpf,Wpfdatagrid,Datagridtextcolumn,是否可以使用elementStyle将DatagridTextColumn IsReadOnly绑定到ViewModel中的属性(此处为ReadOnlyProperty)? 大概是这样的: <DataGridTextColumn.ElementStyle> <Style TargetType="{x:Type TextBlock}"> <Setter Property="IsEnabled" Value="{Binding IsR
<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>
试试这个:
<DataGridTextColumn IsReadOnly="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.IsReadOnlyProperty}" />
创建DataGrid时,通过分配ItemsSource来设置其DataContext。这将DataGrid内的所有控件限制为仅使用其父DataContext中的对象,因此如果要绑定到此DataContext之外的属性,则必须明确声明要更改范围。是否尝试过?当然!!它不会被束缚!!IsReadOnlyProperty不在datagrid Itemsource中,它位于ViewModel中。@raha:您的ItemsSource应该是视图模型的集合,那么该视图模型在哪个上下文中可以访问?但它可以工作!!视图模型位于窗口上下文中!