2010-07-11 61 views
18

如果我创建了DataGridTextColumnIsReadOnly属性的绑定,它不会实现。如果我通过标记来设置它,它会起作用。.Net v4 DataGridTextColumn.IsReadOnly似乎有问题

<DataGridTextColumn IsReadOnly="{Binding IsReferenceInactive}"/> <!-- NOP --> 

<DataGridTextColumn IsReadOnly="True"/> <!-- Works as expected, cell is r/o --> 

IsReferenceInactive属性是一个DP和工作正常(出于测试目的,我绑定了一个复选框,该工作)

这是一个已知的限制?

更新

UUPS,其他比我写的,有在输出窗口消息:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=IsReferenceInactive; DataItem=null; target element is 'DataGridTextColumn' (HashCode=23836176); target property is 'IsReadOnly' (type 'Boolean') 

似乎是这样的一个:

http://connect.microsoft.com/VisualStudio/feedback/details/530280/wpf-4-vs2010-datagrid-isreadonly-does-not-work-with-binding-to-boolean-property

回答

11

DataGridColumn s不是视觉树的一部分,也不参与像这样的绑定。我解决这个问题的方式是使用DataGridTemplateColumn

<DataGridTemplateColumn> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=myProperty}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <TextBox IsEnabled="{Binding Path=myBool}" Text="{Binding Path=myProperty Mode=TwoWay}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
</DataGridTemplateColumn> 

还有其他的解决办法,我发现有点太过分了,但他们确实有效;即:http://blogs.msdn.com/b/jaimer/archive/2008/11/22/forwarding-the-datagrid-s-datacontext-to-its-columns.aspx

+0

好吧,最终我也说过了。在输出窗口中我看起来不够好。对不起,我发布的速度很快。但我不会删除这个问题,也许它会帮助那些也匆忙的人:) – HCL 2010-07-11 11:27:42

+1

没问题;它为Google/Bing提供了更多的饲料,以便在SO上提出问题时找到正确的答案。 – codekaizen 2010-07-11 11:29:51

25

同codekaizen但更简单:

<DataGridTextColumn> 
    <DataGridTextColumn.CellStyle> 
    <Style> 
     <Setter Property="UIElement.IsEnabled" Value="{Binding IsEditable}" /> 
    </Style> 
    </DataGridTextColumn.CellStyle> 
</DataGridTextColumn> 
0

绑定的DataGridTextColumn的仅适用于Text属性,而不是DataGridTextColumn的其他属性。

解决方案: DataGridTextColumn通知DataGrid为每一行和该列创建一个TextBlock。您可以为TextBlock定义样式,并将Style和Style.Key链接到该列的TextBlock(ElementStyle)。

当然,TextBlock现在需要从数据列表中找到对象。它可以通过使用AncestorType = DataGridRow的RelativeSource绑定来实现。 DataGridRow然后提供对象的访问。

事情是这样的:

<Window.Resources> 
    <Style x:Key="IsReadOnlyStyle" TargetType="TextBlock"> 
    <Setter Property="IsReadOnly" 
     Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, 
     Path =Item.NoOutput/> 
    </Style> 
</Window.Resources> 

<DataGrid> 
    <DataGrid.Columns> 
    <DataGridTextColumn Header="Value" Width="*" Binding="{Binding Value}" ElementStyle="{StaticResource IsReadOnlyStyle}"/> 
</DataGrid.Columns> 

复杂吧?我建议你阅读我约的DataGrid详细的文章在格式化: http://www.codeproject.com/Articles/683429/Guide-to-WPF-DataGrid-formatting-using-bindings?msg=5037235#xx5037235xx

祝你好运,你需要它:-)

+0

此解决方案不起作用。 TextBlock没有IsReadOnly属性。 TextBox具有该属性,但只能与EditingElementStyle一起使用,这会产生视觉上不同的结果。 – 2015-10-15 12:51:33

0

如果你喜欢@ codekaizen的解决方案,但将有一个残疾人TextBox的样子,那么这将这样的伎俩:

<DataGridTemplateColumn> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <TextBox IsEnabled="{Binding Path=myBool}" Text="{Binding Path=myProperty}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
    <DataGridTemplateColumn.CellEditingTemplate> 
     <DataTemplate> 
      <TextBox IsEnabled="{Binding Path=myBool}" Text="{Binding Path=myProperty, Mode=TwoWay}" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellEditingTemplate> 
</DataGridTemplateColumn> 
3

我发现这个解决方案,它可以让你绑定到数据时,在DataContext没有继承: http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

添加ŧ他BindingProxy类托马斯写道,这资源添加到您的DataGrid

<DataGrid.Resources> 
    <local:BindingProxy x:Key="proxy" Data="{Binding}" /> 
</DataGrid.Resources> 

现在你可以通过BindingProxyData属性绑定到你的DataContex就像你期望的那样。

<DataGridTextColumn Header="Price" 
        Binding="{Binding Price}" 
        IsReadOnly="{Binding Data.LockFields, Source={StaticResource proxy}}"/> 
相关问题