简体   繁体   English

触发器中的只读附加属性 (WPF)

[英]Read-only attached property in trigger (WPF)

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 :我想在 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:结果,我有2个错误:

Cannot find the Style Property 'Something' on the type 'ReadonlyAttachedProperty.AttachedPropertyHelper'.在“ReadonlyAttachedProperty.AttachedPropertyHelper”类型上找不到样式属性“Something”。 Line 11 Position 16.第 11 行位置 16。

Property 'Something' was not found in type 'TextBlock'.在类型 'TextBlock' 中找不到属性 'Something'。

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;
}

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:我要注意,目前最好使用https://github.com/HavenDV/DependencyPropertyGenerator ,代码将非常简单:

[AttachedDependencyProperty<object, Grid>("Something", IsReadOnly = true)]
public static partial class GridExtensions
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM