Search code examples
wpfgridstylesgridsplitter

Global Style for GridSplitter that depends on its orientation


I'd like to create a global style that would affect every "GridSplitter" of my app to change its cursor appearance when mouse is over.

This code works for vertical GridSplitter:

<Style TargetType="GridSplitter">
  <Setter Property="Cursor" Value="SizeWe"></Setter>
</Style>

But I don't know how to detect its orientation and set the Value to "SizeNs" when appropriate. I do not want to have to change every GridSplitter code. I want one style that could apply for every Gridsplitter without having to set a resource key.

Any idea ?

For all of those who have a XamlParseException : 'Provide value on 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '15' and line position '9'.

Ensure you have proper syntax for Cursor names.


Solution

  • You can use ResizeDirection property in control template triggers and set appropriate cursor.

    <Style TargetType="GridSplitter">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="GridSplitter">                        
    
                    <Rectangle Fill="{TemplateBinding Background}"></Rectangle>
    
                    <ControlTemplate.Triggers>
                        <Trigger Property="ResizeDirection" Value="Columns">
                            <Setter Property="Cursor" Value="SizeNS"></Setter>                                
                        </Trigger>
                        <Trigger Property="ResizeDirection" Value="Rows">
                            <Setter Property="Cursor" Value="SizeWE"></Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>