【问题标题】:WPF Binding with DataContext in DataGridWPF 与 DataGrid 中的 DataContext 绑定
【发布时间】:2012-12-25 01:06:34
【问题描述】:

我有一个 WPF DataGrid,我希望在 DataGrid 中显示一个依赖于 DataGrid 上下文(来自 ViewModel)之外的属性的列。

对于某些标签(没有 "DataContext." ),我在 DataGrid 之外有相同的属性绑定,这很好用。

<DataGrid ItemsSource="{Binding Items.View}" AutoGenerateColumns="False"   x:Name="Overview" >

<DataGridTemplateColumn Header="{lex:Loc Value}" Width="Auto" Visibility="{Binding ElementName=Overview, Path=DataContext.CharacteristicMeasure.Characteristic.CharacteristicType.IsBool,Converter={StaticResource boolToInv}, ConverterParameter=true}" >
  <DataGridTemplateColumn.CellTemplate>
      <DataTemplate>
          <TextBlock Text="{Binding Value}" />
      </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
  <DataGridTemplateColumn.CellEditingTemplate>
      <DataTemplate>
          <TextBox Text="{Binding Value}"  />
      </DataTemplate>
  </DataGridTemplateColumn.CellEditingTemplate>
  </DataGridTemplateColumn>
</DataGridTemplateColumn>

不知何故,这根本不会影响 DataGridTemplateColumn 的 Visibility 属性。我不知道为什么以及如何继续。

更新

我的输出窗口显示以下错误:

    System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=DataContext.CharacteristicMeasure.Characteristic.CharacteristicType.IsBool; DataItem=null; target element is 'DataGridTemplateColumn' (HashCode=15721293); target property is 'Visibility' (type 'Visibility')

更新 2

虽然我在 DataGrid 之外的另一个可见性属性上绑定了相同的属性

    <DockPanel Visibility="{Binding CharacteristicMeasure.Characteristic.CharacteristicType.IsBool,Converter={StaticResource boolToInv}, ConverterParameter=true}" >...

这很好用。

【问题讨论】:

  • 附加调试器并在转换器的 ConvertTo 方法中放置一个断点。这应该验证您的属性是否正确连接,并且转换器正在将其转换为 Visibility 枚举。
  • 转换器未执行。我得到以下输出:System.Windows.Data 错误:2:找不到目标元素的管理 FrameworkElement 或 FrameworkContentElement。 BindingExpression:Path=DataContext.CharacteristicMeasure.Characteristic.CharacteristicType.IsBool;数据项=空;目标元素是“DataGridTemplateColumn”(HashCode=15721293);目标属性是“可见性”(类型“可见性”)
  • 噢!再次被此咬伤 - DataGridColumn 不是视觉元素。 Binding to Visible property DataGridCOlumn in WPF DataGrid 的可能重复项

标签: wpf binding datagrid datacontext


【解决方案1】:

听起来很奇怪,DataGridColumn 类直接继承自 DependencyObject,因此您不能对其属性使用绑定(它没有 SetBinding 方法)。

不知道为什么。

【讨论】:

  • 谢谢。现在我确实理解了这个问题并且已经解决了它。我确实将属性绑定到 TextBox 并显示另一个具有此转换器反转版本的 TextBox。谢谢
猜你喜欢
  • 2013-11-16
  • 2010-11-01
  • 1970-01-01
  • 2016-10-28
  • 2021-08-29
  • 1970-01-01
  • 2015-01-26
  • 2015-01-21
  • 2016-10-04
相关资源
最近更新 更多