使用自己的类型设置AttachedProperty的值

时间:2012-06-30 14:43:00

标签: .net wpf .net-4.0 attached-properties

我创建了一个类似

的类
public class ContextMenuSetting {
    public bool ContextMenuAllowed { get; set; }
    public string GridKey { get; set; }
}

和AttachedProperty一样:

public static readonly DependencyProperty ContextMenuSettingsProperty = DependencyProperty.RegisterAttached("ContextMenuSettings", typeof(ContextMenuSetting), typeof(BarItemLink), null);

public static void SetContextMenuSettings(this BarItemLink target, ContextMenuSetting contextMenuSetting) {
    target.SetValue(ContextMenuSettingsProperty, contextMenuSetting);
}

public static ContextMenuSetting GetContextMenuSettings(this DependencyObject target) {
    return (ContextMenuSetting)target.GetValue(ContextMenuSettingsProperty);
}

我现在在xaml中使用了以下BarItemLink:

<dxb:BarItemLink BarItemName="newActivity"  />

有人可以帮助我,我现在如何设置BarButtonLink的AttachedProperty ContextMenuSettings的值?

编辑 - 更新解决方案:

在Charleh的帮助下,我找到了解决方案:

<dxb:BarItemLink BarItemName="newActivity">
   <pcce:BarItemLinkExtensions.ContextMenuSettings >
       <pcce:ContextMenuSettings ContextMenuAllowed="True" GridKey="hallo"/>
   </pcce:BarItemLinkExtensions.ContextMenuSettings>
</dxb:BarItemLink>

1 个答案:

答案 0 :(得分:0)

您是否在XAML中包含了ContextMenuSetting类命名空间?

xmlns:ctxmenu="SomeNameSpace"

<dxb:BarItemLink BarItemName="blah" ctxmenu:ContextMenuSetting.ContextMenuSettings="blah" />

编辑:如果要设置自定义类型,可以使用TypeConverter(如果要输入字符串值)或使用扩展语法

<dxb:BarItemLink BarItemName="blah">
   <ctxMenu:ContextMenuSetting.ContextMenuSettings SomeProperty="SomeValue" />
</dxb:BarItemLink>

或TypeConverter如果你想这样做:

<dxb:BarItemLink BarItemName="blah" ctxmenu:ContextMenuSetting.ContextMenuSettings="blah" />

将解释字符串值“blah”并使用TypeConverter在代码中创建正确的类型

http://msdn.microsoft.com/en-us/library/aa970913.aspx