aKzenT
aKzenT

Reputation: 7895

Disable column reordering in WPF with Style

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>

Upvotes: 3

Views: 4706

Answers (5)

Call Me Juan
Call Me Juan

Reputation: 11

Just simply add this attributes to your datagrid tag

CanUserResizeColumns="False"

Upvotes: 1

ciuppinho
ciuppinho

Reputation: 61

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.

Upvotes: 6

aKzenT
aKzenT

Reputation: 7895

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.

Upvotes: 0

RredCat
RredCat

Reputation: 5421

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.

Upvotes: 0

Tilak
Tilak

Reputation: 30698

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

Upvotes: 0

Related Questions