1
votes

I got a WPF DataGrid and in the DataGrid i want to have a column displayed dependent on a property outside of the DataGrid Context (from the ViewModel).

I have the same Property binding outside of the DataGrid for some labels (without the "DataContext." ) and this works fine.

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

Somehow this isnt effecting the Visibility property of the DataGridTemplateColumn at all. I dont know why and how to continue.

UPDATE

My output windows shows the following error:

    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')

UPDATE 2

though i got the same Property Binded on another Visibility Property outside of the DataGrid

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

and this works fine.

1
Attach your debugger and put a breakpoint in your converter's ConvertTo method. That should verify that your property is wired up correctly and that the converter is converting it to a Visibility enum.Gishu
Converter isnt executed. i get the following output: 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')JuHwon
Doh! Bitten by this again - DataGridColumn isn't a visual element. possible duplicate of Binding to Visible property DataGridCOlumn in WPF DataGridGishu

1 Answers

1
votes

As strange as it sounds, the DataGridColumn class inherits directly from DependencyObject, so you can't use bindings on its properties (it has no SetBinding method).

Can't figure out why.