2

a little background first. We have some legacy UI controls which are defined in text files and we are parsing the same text files to create xaml.

One of the controls is similar to a WPF ListView and the text file can hold a collection of columns which have their own properties. One of these properties denotes if a column is to have a fixed width, which WPF does not automatically provide.

A picture speaks a thousand words, so here is an example of a parsed file containing a ListView with four columns:

Basic layout

For simplicities sake I have used coloring for demonstration purposes. The first column with the red text, is the column with a fixed width.

To achieve this, during the parsing, each GridViewColumnHeader receives a Style, which in itself can be modified, depending on the flags (i.e. "IsFixed") contained in the properties for that column.

In order to fix the width of a column, I have created a Style which adds the MinWidth and MaxWidth setters to the style. Please note that the ColumnResize is a custom enum.

private void _ModifyColumnResize(ref Style style, ColumnResize columnResize, int width)
{
    if (columnResize == ColumnResize.IsFixed)
    {
        var minWidth = new Setter(MinWidthProperty, (double)width);
        var maxWidth = new Setter(MaxWidthProperty, (double)width);
        var fixedColor = new Setter(ForegroundProperty, Brushes.Red);

        style.Setters.Add(minWidth);
        style.Setters.Add(maxWidth);
        style.Setters.Add(fixedColor);
    }
}

This all seems to work fine, double-clicking on the column grip does not automatically change the width to that of its content (as with the other three columns). The problem is if I click on the gripper then attempt to resize, then I see this:

After resizing

As you can see, the column itself has the same width, but to the left and right of the column, spacing appears - which is not wished.

My question is, can I stop this behavior through styling? Ideally I would not like to have the gripper visible for a column which is not to allow resizing, but I am unsure as to how to accomplish that as the Style would be created in code-behind (during the parsing) and not in xaml.

Thanks for your time!

5
  • You said 'Ideally I would not like to have the gripper visible for a column which is not to allow resizing'. Wanted to know what operations a user is allowed to perform on Column headers in your app? Oct 8, 2014 at 13:32
  • Hi there, they are supposed to be able to sort and reassign the column layout (drag&drop) which I don't see as being a problem. The problem - for now - is that in the old controls, they were able to say that a column had a fixed width hence the question. Thank you. Oct 8, 2014 at 13:37
  • Have you checked out this. As per your scenario, I think you need to modify the Column header control template. We cannot disable header since the user will interact with header for other things (sort/drag/drop) Oct 8, 2014 at 14:03
  • For setting this custom style for header in your parser, you can define required style in application resources and set it the ColumnHeaderContainerStyle property of control. This link may help. Oct 8, 2014 at 14:09
  • With your help I have found a way to do this. I'm just going to clean up then write the answer here shortly with credit going to you. Oct 8, 2014 at 14:32

1 Answer 1

0

With thanks to @DareToExplore here is the solution:

I grabbed a copy of the default template for the GridViewColumnHeader from MSDN and removed the part PART_HeaderGripper. This Style is not to be the default for every column and is only to be used for a column which should not be resizable.

Whilst parsing the text file, for every column which may be resized, nothing happens, as this is the default behavior.

For the non-resizable column, I grab the Style resource from the App.xaml, then assign that Style to the HeaderContainerStyle property of that GridViewColumnHeader like this:

if (parsedColumn.ColumnResize == ColumnResize.IsFixed)
{
    var style = this.FindResource("GridViewColumnHeader_NoResize");
    gridViewColumn.HeaderContainerStyle = style as Style;
}

For now, I will leave the Style in the App.xaml and grab it as needed.

I hope this is detailed enough and it helps someone along the way.

Thanks again @DareToExplore!

4
  • Glad that I could help. You can accept this as your answer. Oct 8, 2014 at 16:37
  • Apparently, I need to wait until tomorrow to accept this so I will wait a day! I do hope you get credit for this, thank you once again. Oct 9, 2014 at 6:08
  • Ok. Credit- Well your 'thank you' is a credit for me. In terms of the stackflow points credit, I don't think any points will be credited. That is because I did not post an answer, which you could accept. I just guided you. I am not sure, if you can up vote by comments and that would add something. Oct 9, 2014 at 6:23
  • As someone who is fairly new to WPF could you please post the appropriate resource xaml?
    – David
    May 11, 2016 at 17:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.