0

I have a ListView whose View is that of a GridView i.e.:

<ListView>
    <ListView.View>
        <GridView />
    </ListView.View>
</ListView>

With this example:

Before dragging

The two columns with red text have a Style applied where the IsHitTestVisible property is set to false - which prevents the user from moving those two columns.

It is however possible, and it should be, for the user to drag the other three columns amongst themselves.

Unfortunately, with this scenario, the user can move one of the three moveable columns before either the first or second unmovable columns, thus moving a column which should not be movable, like here:

After dragging

My aim is to keep the unmovable columns where they are whilst allowing the movable columns to be moved amongst themselves.

The amount of unmovable columns is variable, it is possible that the GridView has no fixed columns whatsoever, it is also possible however, that there may be two - as shown here - or even more (for larger tables).

The question therefore would be, how can I hinder the user moving columns before a column which itself cannot be moved?

Thank you for your time!

Community
  • 1
  • 1
user3235445
  • 155
  • 1
  • 10

1 Answers1

0

After finding this post from @Rida:

How to disable Moving/ReOrdering of ListView Header in WPF?

I decided to solve my problem with a DependencyProperty called FixedColumns and which was an integer.

public class ColumnsExtension : DependencyObject
{
    public static readonly DependencyProperty FixedColumnsProperty =
        DependencyProperty.RegisterAttached("FixedColumns", typeof(int),
        typeof(ColumnsExtension), new PropertyMetadata(0));

    public static int GetFixedColumns(DependencyObject obj)
    {
        return (int)obj.GetValue(FixedColumnsProperty);
    }

    public static void SetFixedColumns(DependencyObject obj, int value)
    {
        obj.SetValue(FixedColumnsProperty, value);
    }
}

I parse the text file containing the data which describes the columns, if I encounter a column whose position is to be fixed, then I increase the number of fixed columns:

foreach (var parsedColumn in parsedColumns)
{
    var fCols = this.TestLV.GetValue(ColumnsExtension.FixedColumnsProperty);

    // increase the number of fixed columns
    if (parsedColumn.ColumnResize == ColumnResize.IsFixed)
        this.TestLV.SetValue(ColumnsExtension.FixedColumnsProperty, (int)fCols + 1);

    // ...
}

Once the parsing has finished, I subscribe to the CollectionChanged EventHandler and the code is basically as what @Rida proposed but instead of checking for 0, I check to see if the NewStartingIndex is smaller than the number of fixed columns.

If this is the case, then I simply cancel the move.

The result is that I can now have this constellation with two fixed columns:

Original state

Where it is possible for me to drag the last three columns between themselves, but not before a fixed column.

After dragging

Please note that the drag adorner is still visible when attempting to drag a column before a fixed column but the drop simply fails which is the main thing for me. With time I may try and change the drop adorner for this situation.

I hope this helps someone along the way, as the code from @Rida helped me.

Community
  • 1
  • 1
user3235445
  • 155
  • 1
  • 10