I have a DataGrid. I want to decide when to collapse a column and when to show it.
This is my code:
<UserControl.Resources>
<ResourceDictionary>
<FrameworkElement x:Key="ProxyElement" DataContext="{Binding}" />
</ResourceDictionary>
<UserControl.Resources>
<DataGridTextColumn.Visibility>
<MultiBinding Converter="{StaticResource MyMultiValueConverter}">
<Binding Source="{StaticResource ProxyElement}" Path="DataContext.MyPropertyInViewModel" />
<Binding Source="1"/>
</MultiBinding>
</DataGridTextColumn.Visibility>
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//Do the conversion
}
I need the proxy element to access the view model from an element that doesn't belong to the visual tree.
In the MultiBinding, the second binding works. In the converter I receive the value 1, but the problem is with the first element. I don't get the property of the view model, that it is a string. I get a DependencyProperty.UnsetValue.
How can I pass a property of my view model to the multi-value converter?
The
ProxyElementwill not bind the data context inResourcesas it is not part of the visual tree. To make this work, define theFrameworkElementanywhere in the visual tree, e.g. like below in aGrid. TheDataContextis inherited, but you can also set it explicitly. Set theVisibilityof the proxy toCollapsed, so it is hidden.Reference it using
x:Reference, sinceElementNamebindings only work in the visual tree, but columns are not part of it.A better way is to use a
Freezableas binding proxy. Those can access the data context even outside of the visual tree. See this related post that shows an approach with a customBindingProxy, that also works inResourcesand withoutx:Reference.