6

I have a toolkit:DataGrid (from Codeplex).

It is starting to have many different styles:

  • one for switching it on and off based on a variable in the ViewModel
  • one for visual styling
  • one for some other triggers, etc.

Do all of these need to be in one big style, is that the only way to do it? Or can I have multiple styles and attach them as I need them? Is there anyway to do this so that you can e.g. swap styles in code?

Styles:

<Style x:Key="CodePlexDataGridSwapper" TargetType="toolkit:DataGrid">
    <Style.Triggers>
        <DataTrigger Binding="{Binding WhichGrid}" Value="Infragistics">
            <Setter Property="toolkit:DataGrid.Visibility" Value="Collapsed"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style x:Key="ToolkitDataGridLayout" TargetType="toolkit:DataGrid">
    <Setter Property="Background" Value="Yellow"/>
</Style>

PSEUDO-CODE:

<toolkit:DataGrid 
    Style="{StaticResource CodePlexDataGridSwapper, ToolkitDataGridLayout}" 
    ItemsSource="{Binding Customers}"/>
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

1 Answers1

11

You may want to look at the BasedOn property of the Style class. Basically, it allows you to inherit one style from another. The 'child' style will have all of the setters and triggers from the parent one (unless it has setters overriding the parent's ones) plus it will have obviously its own setter and triggers.

Maybe this info will be helpful for you.

arconaut
  • 3,227
  • 2
  • 27
  • 36
  • 1
    +1 because it somewhat helps, but it still does not help in applying/merging more than two separate sets of styles. Unfortunatelly, it's quite probable that except for simple based-on semi-inheritance there's no way to apply 'many' of them. – quetzalcoatl Apr 06 '13 at 13:46
  • answer may be here:http://stackoverflow.com/questions/16096/how-to-apply-multiple-styles-in-wpf – Admiral Land Mar 02 '16 at 06:38