1

I have a DataGrid bound to a ViewModel's property of type ObservableCollection. Inside DataGrid I have several DataGridTextColumns bound to item of the ObservableCollection. I need to access parent DataContext (ViewModel) to set Visibility property of one of the DataGridTextColumns. There is a solution suggested over the internet:

{Binding DataContext.IsColumnVisible, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MyControl}}}

IsColumnVisible property is of type System.Windows.Visibility. Such solution works for other properties like DataGridTextColumn.Binding, but doesn't for DataGridTextColumn.Visibility! Is there solution for Visibility?

EDIT: I finally adopted the following solution: Bind datagrid column visibility MVVM

Community
  • 1
  • 1
user10101
  • 1,704
  • 2
  • 20
  • 49

3 Answers3

2

DataGrid-columns are abstract objects, you cannot target them using ElementName (lacking namescope) or RelativeSource (not in the visual tree).

Additionally DataGrid-columns have no DataContext...

H.B.
  • 166,899
  • 29
  • 327
  • 400
  • Thank you for the explanation! But is there not too complex way to accomplish my goal? – user10101 Jan 13 '12 at 17:34
  • @user835103: Well, as columns do not have a DataContext, what is the DataContext you try to target? The item's or the `DataGrid`'s? – H.B. Jan 13 '12 at 17:49
  • The DataGrid's. Sorry for delay, I didn't have access to my SA account. – user10101 Jan 16 '12 at 10:39
  • 1
    @user835103: See [this answer](http://stackoverflow.com/a/6858917/546730) of mine which should be easily adapted to your situation (move things into `DataGrid.Resources` and target the `DataGrid` and not the `Window`) – H.B. Jan 16 '12 at 23:19
  • Thank you. Unfortunately I cannot use x:Reference because target .NET Framework 3.5. I finally adopted the following solution: http://stackoverflow.com/questions/7711275/bind-datagrid-column-visibility-mvvm. – user10101 Jan 17 '12 at 10:46
  • @user835103: That of course is an alternative which i would have suggested as well given the circumstances. I prefer the *x:Reference* method where usable, seems a lot cleaner to me. – H.B. Jan 17 '12 at 10:54
0

You can use a BooleanToVisibilityConverter to convert the Parent's Visibility property to a Boolean value.

http://msdn.microsoft.com/en-us/library/system.windows.controls.booleantovisibilityconverter.aspx

Jeff
  • 35,755
  • 15
  • 108
  • 220
  • I don't need BooleanToVisibilityConverter here. ViewModel.IsColumnVisible and DataGridTextColumn.Visibility are both of type System.Windows.Visibility. Actually I temporarily changed ViewModel.IsColumnVisible type from bool to Visibility to get rid of converters for simplicity. – user10101 Jan 13 '12 at 17:26
0

Bind the Visibility property of your column to a property of your view model. See the following SA question.

Another thing is: use the BooleanToVisibilityConverter class. It's not good your view model exposes a property of a type (Visibility) that is closely tied to the view. This introduces a coupling between your view and your view model that should be avoided.

Exposes a bool, then let the view adapts itself to its view model using the converter.

Community
  • 1
  • 1
ken2k
  • 48,145
  • 10
  • 116
  • 176