4

I'm a using a ListView with GridViewColumn in my C# WPF application.

For some columns I use a Visibility manager (this one). But When column 1 and 3 are visible and column 2 not, resizing column 1 makes column 2 appear if cursor is too right.

Is it possible to disable resizing (and unactibe cursor) on column if IsVisible is false ?

I saw that post, but I can't use it on the GridViewColumnVisibilityManager, my columns are not always fixed.

The part of GridViewColumnVisibilityManager interesting :

private static void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    GridViewColumn gc = d as GridViewColumn;
    if (gc == null)
        return;

    if (GetIsVisible(gc) == false)
    {
        originalColumnWidths[gc] = gc.Width;
        gc.Width = 0;
        // Fix Size there
    }
    else
    {
        if (gc.Width == 0)
        {
            gc.Width = originalColumnWidths[gc];
            //UnFix
        }
    }
}

Is it possible to programmatically remove gripper from columns ?

One is to restyle GridViewColumnHeader to remove the gripper inside its Template

Community
  • 1
  • 1
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93

2 Answers2

2

Finally found a solution :

In App.xaml :

<Style x:Key="disabledColumn" TargetType="GridViewColumnHeader">
    <Setter Property="IsEnabled" Value="False"/>
</Style>

In GridViewColumnVisibilityManager.cs :

public class GridViewColumnVisibilityManager
{
    static Dictionary<GridViewColumn, double> originalColumnWidths = new Dictionary<GridViewColumn, double>();
    static Dictionary<GridViewColumn, Style> originalColumnHeader = new Dictionary<GridViewColumn, Style>();

    public static bool GetIsVisible(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsVisibleProperty);
    }

    public static void SetIsVisible(DependencyObject obj, bool value)
    {
        obj.SetValue(IsVisibleProperty, value);
    }

    public static readonly DependencyProperty IsVisibleProperty =
            DependencyProperty.RegisterAttached("IsVisible", typeof(bool), typeof(GridViewColumnVisibilityManager), new UIPropertyMetadata(true, OnIsVisibleChanged));

    private static void OnIsVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        GridViewColumn gc = d as GridViewColumn;
        if (gc == null)
            return;

        if (GetIsVisible(gc) == false)
        {
            originalColumnWidths[gc] = gc.Width;
            gc.Width = 0;

            originalColumnHeader[gc] = gc.HeaderContainerStyle;
            gc.HeaderContainerStyle = Application.Current.FindResource("disabledColumn") as Style;
        }
        else
        {
            if (gc.Width == 0)
            {
                gc.Width = originalColumnWidths[gc];
                gc.HeaderContainerStyle = originalColumnHeader[gc];
            }
        }
    }
}
A.Pissicat
  • 3,023
  • 4
  • 38
  • 93
1

For a purely XAML solution, you can do this:

<Style x:Key="GridHeaderStyle" TargetType="GridViewColumnHeader">
    <!-- any other things you want -->
    <Setter Property="IsEnabled"
            Value="{Binding RelativeSource={RelativeSource Self},
                            Path=Column.(local:GridViewColumnVisibilityManager.IsVisible)}" />
</Style>

Or this (which one is better depends on what other things you have in the style, and whether you have an IsVisible or an IsHidden or something more complex):

<Style x:Key="GridHeaderStyle" TargetType="GridViewColumnHeader">
    <!-- any other things you want -->
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
                               Path=Column.(local:GridViewColumnVisibilityManager.IsVisible)}"
                     Value="False">
            <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>
</Style>

And then set:

<GridView ColumnHeaderContainerStyle="{StaticResource GridHeaderStyle}" ...>

(Or you can do the usual things, like making this the default style in your App.xaml.)

Miral
  • 12,637
  • 4
  • 53
  • 93