Read-only attached property in trigger (WPF)

attached-properties, c#, readonly, triggers, wpf

Solution

I don't know if there is something special with your read-only attached property, but if you declare it in default manner it works:

public class AttachedPropertyHelper : DependencyObject
{
    public static int GetSomething(DependencyObject obj)
    {
        return (int)obj.GetValue(SomethingProperty);
    }

    public static void SetSomething(DependencyObject obj, int value)
    {
        obj.SetValue(SomethingProperty, value);
    }

    // Using a DependencyProperty as the backing store for Something. This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SomethingProperty =
        DependencyProperty.RegisterAttached("Something", typeof(int), typeof(AttachedPropertyHelper), new UIPropertyMetadata(0));
}

EDIT

If you want the same as a readonly attached property you change it to:

public class AttachedPropertyHelper : DependencyObject
{
    public static int GetSomething(DependencyObject obj)
    {
        return (int)obj.GetValue(SomethingProperty);
    }

    internal static void SetSomething(DependencyObject obj, int value)
    {
       obj.SetValue(SomethingPropertyKey, value);
    }

    private static readonly DependencyPropertyKey SomethingPropertyKey =
        DependencyProperty.RegisterAttachedReadOnly("Something", typeof(int), typeof(AttachedPropertyHelper), new UIPropertyMetadata(0));

    public static readonly DependencyProperty SomethingProperty = SomethingPropertyKey.DependencyProperty;
}

Problem

I have a problem with read-only attached property. I defined it in this way : ``` public class AttachedPropertyHelper : DependencyObject { public static readonly DependencyPropertyKey SomethingPropertyKey = DependencyProperty.RegisterAttachedReadOnly("Something", typeof(int), typeof(AttachedPropertyHelper), new PropertyMetadata(0)); public static readonly DependencyProperty SomethingProperty = SomethingPropertyKey.DependencyProperty; } ``` And I want to use it in XAML : ``` <Trigger Property="m:AttachedPropertyHelper.Something" Value="0"> <Setter Property="FontSize" Value="20"/> </Trigger> ``` But compiler doesnt want to work with it. In result, I have 2 errors: Cannot find the Style Property 'Something' on the type 'ReadonlyAttachedProperty.AttachedPropertyHelper'. Line 11 Position 16. Property 'Something' was not found in type 'TextBlock'.

Original source