附加属性作为XAML元素

Hou*_*ell 10 c# silverlight xaml windows-phone-7

我有一类附加属性:

public static class XamlProps
{
    #region Attached Properties

    private static readonly DependencyProperty FooProperty = DependencyProperty.RegisterAttached(
        "Foo",
        typeof(string),
        typeof(XamlProps),
        null);

    public static void SetFoo(DependencyObject obj, string action)
    {
        obj.SetValue(FooProperty, action);
    }
}
Run Code Online (Sandbox Code Playgroud)

我在我的XAML中使用这些属性:

<Border me:XamlProps.Foo="Foo to the Bar">
Run Code Online (Sandbox Code Playgroud)

但是现在我想在这个属性中有一个更大的值,所以我想把它用作一个元素:

<Border>
    <me:XamlProps.Foo>Foo to the Bar</me:XamlProps.Foo>
</Border>
Run Code Online (Sandbox Code Playgroud)

但是现在Silverlight不再调用SetFoo()了.我如何让它工作?

在Windows Phone 7上,如果重要的话.

Dam*_*ian 5

如果使用该语法,则需要指定类型:

<Border>
    <me:XamlProps.Foo>
        <sys:String>Foo to the Bar</sys:String>
    </me:XamlProps.Foo>
</Border>
Run Code Online (Sandbox Code Playgroud)

sys命名空间映射到System的位置.你还需要定义GetFoo ...

可能是复制粘贴错字,但在注册时

typeof(XamlActions)
Run Code Online (Sandbox Code Playgroud)

应该

typeof(XamlProps)
Run Code Online (Sandbox Code Playgroud)