I am trying to hide colummns in a Datagrid
(bound to a DataTable
) with DataTrigger
. So that the user can choose which columns are displayed. My problem is that this only works once. Normaly I would say that I need the INotifyPropertyChanged
, however my ContextMenu
is defined in XAML so I am not sure if and how to do that.
Here my XAML
<ContextMenu >
....
<MenuItem Header="Apply" x:Name="ButtonApply" />
</ContextMenu>
<DataGrid ItemsSource="{Binding Path=TabDataTable}" AutoGenerateColumns="True" IsReadOnly="True" SelectionUnit="CellOrRowHeader" Margin="-5,-4,-5,-4" AlternatingRowBackground="Gainsboro" x:Name="MainDataGrid">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=ButtonApply, Path=IsPressed}" Value="True">
<Setter Property="Visibility">
<Setter.Value>
<!--<Binding Path=" DataContext.IsTemplateColumnVisibile, Source={StaticResource ProxyElement}, Converter={StaticResource BooleanToVisibilityConverter}}" />-->
<MultiBinding Converter="{StaticResource BooleanToVisibilityConverter}">
<Binding Path="Column"
RelativeSource="{RelativeSource Self}"/>
<Binding Path="DataContext.visibility"
RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
Here the Converter
: (Sry for the misleading name)
public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value[0] == null)
{
return null;
}
if ((((ObservableCollection<bool>)value[1]).ToArray())[((System.Windows.Controls.DataGridTextColumn)value[0]).DisplayIndex])
{
//Column Visibility
((System.Windows.Controls.DataGridTextColumn)value[0]).Visibility = Visibility.Visible;
//Header Visibility
return Visibility.Visible;
}
((System.Windows.Controls.DataGridTextColumn)value[0]).Visibility = Visibility.Collapsed;
return Visibility.Collapsed;
}
If there is a better way to accomplish this, please feel free to point me in the right direction.