0

I'm working on a personal library to define styles. I have created some DependecyProperty to manage style size. Exemple of the style when it works

Here is a part of the dependencies I have created :

public class CheckboxExtensions : CheckBox
{
    public static readonly DependencyProperty CheckHeightProperty =
        DependencyProperty.RegisterAttached(
            "CheckHeight",
            typeof(double),
            typeof(CheckboxExtensions),
            new PropertyMetadata(OnHeightChange));

    public static void SetCheckHeight(UIElement element, double value)
    {
        element.SetValue(CheckHeightProperty, value);
    }

    public static readonly DependencyProperty LeftColorProperty =
        DependencyProperty.RegisterAttached(
            "LeftColor",
            typeof(SolidColorBrush),
            typeof(CheckboxExtensions));

    public static void SetLeftColor(UIElement element, SolidColorBrush value)
    {
        element.SetValue(LeftColorProperty, value);
    }

    public static SolidColorBrush GetLeftColor(UIElement element)
    {
        return (SolidColorBrush)element.GetValue(LeftColorProperty);
    }
}

And I can define colors like this in my style :

<Setter Property="extensions:CheckboxExtensions.LeftColor" Value="{DynamicResource Foreground}" />
<Setter Property="extensions:CheckboxExtensions.RightColor" Value="{DynamicResource ControlBackground.Flash}" />

When I use it directly in window, it's working but I have a design time that isn't showing the "bullet" enter image description here

But I have a bigger problem. When I use it in a UserControl library and load that UserControl into an application. I got the same result as in design time. No color at all but size are correctly set.

How can I fix this ?

Thibaud
  • 377
  • 1
  • 2
  • 15
  • As a note, a class that declares attached properties does not need to be derived from any DependencyObject subclass (like yours from CheckBox). It might even be static: `public static class CheckboxExtensions { ... }` – Clemens Jul 16 '19 at 13:42
  • @Thibaud: So it works when it's not in a library? Are you sure that the resources are resolved? By the way, you shouldn't name `Brush` properties anything that ends with "Color". – mm8 Jul 16 '19 at 13:46
  • @Clemens you are right, I can make it static but didn't change anything in the result – Thibaud Jul 16 '19 at 13:48
  • There is also no good reason to call a Brush a Color. Call it `LeftBrush` and use Brush instead of SolidColorBrush as property type. – Clemens Jul 16 '19 at 13:49
  • @mm8 Yes resources are resolved and on a application I've created to test the styles it works. But not if I nested it in second library – Thibaud Jul 16 '19 at 13:50
  • @mm8 well... that's weird, on a new project it's working good :/ – Thibaud Jul 16 '19 at 14:02

0 Answers0