0

I am working on a WPF application where I by now have a lot of different Resource Dictionaries comprising a huge amount of styles. Many of those styles describe brush settings for borders, backgrounds, etc. Until now, every formatting is in a separate style.

However, what I would like to do (if possible) is to "group" different styles that belong together into a "parent" style - thereby being able to make it a little more manageable which styles concerns which controls.

For example, for a GroupBox, the settings would ideally be registered in a style like this:

<Style x:Key="GroupBoxFormat">
    <Style.Resources>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1" x:Key="ElementGradientBrush">
            <GradientStop Color="AntiqueWhite" Offset="0" />
            <GradientStop Color="Tan" Offset=".7" />
        </LinearGradientBrush>

        <Border x:Key="BorderFormat" BorderThickness="2">
            <Border.Background>
                <SolidColorBrush Color="Gold"></SolidColorBrush>
            </Border.Background>
            <Border.BorderBrush>
                <SolidColorBrush Color="Black" ></SolidColorBrush>
            </Border.BorderBrush>
        </Border>

        <FontFamily x:Key="TitleFontFamily">
            Arial
        </FontFamily>

        <FontWeight x:Key="TitleFontWeight">
            Bold
        </FontWeight>

        <!-- Other formats go here... -->

    </Style.Resources>
</Style>

As seen in the listing, all separate resources within the style have x:Key attributes specified. As this is seemingly required (as VS complains if it is missing), there must obviously be a reason for that.

From elsewhere in the XAML code, the style can be referenced with {StaticResource GroupBoxFormat}. However, if I want to refer from outside to any of the "inner" properties, say, "BorderFormat" - how do I do that? Something like "dot" notation within C# for accessing object members like SomeObject.SomeMember. Staying in the example, It could be illustrated like "GroupBoxFormat.BorderFormat". I just need the "XAML" way of doing that.

Suresh
  • 4,091
  • 28
  • 35
L. H.
  • 83
  • 2
  • 9
  • Here is a very good article about [WPF resources](http://www.wpf-tutorial.com/wpf-application/resources/). – Suresh Jun 19 '18 at 23:53

1 Answers1

0

There is nothing like what you're asking for with the "dot notation within C#". The only thing I can suggest is to define global styles in your App.xaml without defining a Key but defining a TargetType:

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Margin" Value="3"/>
    <Setter Property="MinWidth" Value="110"/>
    <Setter Property="IsReadOnly" Value="True"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="TextWrapping" Value="Wrap"/>
 </Style>

Now all of your TextBox controls will have these properties defined. If you'd like a specific TextBox to have a different style you must define another TextBox style but set the x:Key attribute as well as the TargetType and then reference the Key.

<TextBox Style{StaticResource YourKey}/>
rejy11
  • 742
  • 2
  • 12
  • 25
  • Thanks for your reply. Maybe I'm a little naive, but if it is not possible to "reach" the inner properties within a style directly from outside, what is the point of having the separate "x:Key" specifications for each of those elements? They're obviously required for something. – L. H. Jun 20 '18 at 08:51
  • I understand what you're trying to achieve by having this style container 'GroupBoxFormat' and then individual style components defined inside, but this is not the right way to do things. Everything you have defined within `` can only be used within 'GroupBoxFormat' and not elsewhere in your app. I suggest viewing this SO question about styling a GroupBox https://stackoverflow.com/questions/9361903/styling-a-groupbox – rejy11 Jun 20 '18 at 09:02
  • I understand. Thanks for the help. – L. H. Jun 20 '18 at 10:53