3

I have an application with a lot of ListView controls using the GridView layout. All of these use a custom Style customStyle. I want to disable column reordering in all of these ListView controls.

Can I do this without touching all of the controls using the existing Style? I tried this, but it doesn't seem to work:

<Style x:Key="customStyle"
       BasedOn="{StaticResource ResourceKey={x:Type ListView}}"
       TargetType="{x:Type ListView}">
    <Setter Property="GridView.AllowsColumnReorder" Value="False" />
    ...
</Style>

Thanks!

Update: If this is not possible, is there a way to disable the column reordering by default in my application?

Update: I also tried this, which doesn't work. Probably because GridView doesn't have a Style property.

<Style TargetType="{x:Type GridView}">
    <Setter Property="AllowsColumnReorder" Value="False" />
    ...
</Style>
aKzenT
  • 7,775
  • 2
  • 36
  • 65

5 Answers5

6

Here's what you should have done:

In this style definition

<Style x:Key="{x:Static GridView.GridViewScrollViewerStyleKey}" TargetType="ScrollViewer">

There is a

<GridViewHeaderRowPresenter

And it has an attribute AllowsColumnReorder hard code it to false.

...

I know ... Two year old thread. But I bet people still read it.

ciuppinho
  • 61
  • 1
  • 3
1

Just simply add this attributes to your datagrid tag

CanUserResizeColumns="False"
0

Check this answer.

Try to set default style of all GridView in your pages with this property.

EDIT: Create new class that inherit from GridView. And change value of AllowsColumnReorder property in constructor or change default value of dependency property AllowsColumnReorderProperty in static constructor to false. And use this class for your list views in all your pages.

EDIT 2: The best way to override dependency property - are used OverrideMetadata method in static constructor.

I have tired to use something like

<Setter Property="(ListView.View).(GridView.AllowsColumnReorder)" Value="False"/>

But it doesn't compile.

Community
  • 1
  • 1
RredCat
  • 5,259
  • 5
  • 60
  • 100
  • I tried that. However, GridView doesn't has a Style property. – aKzenT Jul 05 '12 at 18:10
  • This is a very ugly solution. Not only do I have to touch every single ListView in the application which is exactly what I wanted to avoid, I also have to create a new kind of GridView only to change a default value. There hast to be anotehr way. – aKzenT Jul 05 '12 at 21:48
  • Agree, it looks not very smart. But I couldn't set child property in `Style` and can't figure out what is wrong. – RredCat Jul 06 '12 at 09:53
  • And if you _have_ to touch every `ListView`, then it is easier to just do `...` and be done with it. – Jesse Chisholm May 01 '15 at 19:16
0

You can remove x:key in your example, move it to app.xaml, and make it default style for all GridView in your app.

Tilak
  • 30,108
  • 19
  • 83
  • 131
  • GridView doesn't seem to allow styling in this way. – aKzenT Jul 05 '12 at 18:11
  • You have to check the GridView default template, and find the way through. For getting the template, open in Expression blend, and Click on `Template-> Edit a copy` – Tilak Jul 06 '12 at 03:43
0

I solved this using a attached dependency property for the ListView control. This property will then set the property on the GridView. From my Style I can then set this attached dependency property. Still not a nice solution, but it works.

aKzenT
  • 7,775
  • 2
  • 36
  • 65