Read-only attached property in trigger (WPF)
Asked Answered
R

2

7

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'.

Rheims answered 18/5, 2012 at 8:7 Comment(0)
E
6

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;
}
Encincture answered 18/5, 2012 at 14:15 Comment(2)
In which way? What do you want to achieve?Encincture
Sorry for my slow-wittedness. You still need a public get accessor for the CLR wrapper. Then it will work.Encincture
K
0

I want to note that at the current time it is better to use https://github.com/HavenDV/DependencyPropertyGenerator, the code will be extremely simple:

[AttachedDependencyProperty<object, Grid>("Something", IsReadOnly = true)]
public static partial class GridExtensions
{
}
Kin answered 24/6, 2022 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.