0

我创建了一个类

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>
4

1 回答 1

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

于 2012-06-30T14:53:35.730 回答