Search code examples
c#.netwpf

Using FrameworkPropertyMetadataOptions Flags for Custom Control


Upon browsing the source code for WPF I stumbled across a few dependency properties that use certain FrameworkPropertyMetadataOptions in their definitions. I am creating a custom window control with its own template design. One of the dependency properties I am creating is to set the background color of the title bar:

public static readonly DependencyProperty TitleBarBackgroundProperty =
    DependencyProperty.Register(
        "TitleBarBackground",
        typeof(Brush),
        _typeofThis,
        new FrameworkPropertyMetadata(
            null,
            FrameworkPropertyMetadataOptions.AffectsRender |
            FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender));

I'm trying to understand if FrameworkPropertyMetadataOptions.SubPropertiesDoNotAffectRender is good to have for my property. I am setting these two flags because this is how a Panel's background dependency property is defined here.

I found the following article on Microsoft's learn: https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkpropertymetadataoptions?view=windowsdesktop-7.0

My question is what qualifies as a "sub-property" and do I need it for my property I am defining if all I'm doing with it is binding it to a Grid's background property (which I assume already has these flags since it inherits the panel's background property). Any help would be greatly appreciated, thank you.


Solution

  • If the property only serves as source property of a Binding there is no need to set any property metadata at all. If re-rendering is required, the target object of the Binding should initiate it.

    The declaration of the dependency property identifier field should simply look like this:

    public static readonly DependencyProperty TitleBarBackgroundProperty =
        DependencyProperty.Register(
            nameof(TitleBarBackground),
            typeof(Brush),
            typeof(YourControl));