JuHwon
JuHwon

Reputation: 2063

WPF Binding with DataContext in DataGrid

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.

Upvotes: 1

Views: 3396

Answers (1)

Nicolas Repiquet
Nicolas Repiquet

Reputation: 9255

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.

Upvotes: 1

Related Questions