简体   繁体   English

在wp7 / silverlight中,有什么方法可以在上下文菜单旁边放置对号吗?

[英]Is there any way to put a checkmark beside a context menu in wp7 / silverlight?

I have a several MenuItem's inside a ContextMenu in my wp7 app using silverlight. 我使用Silverlight在wp7应用程序的ContextMenu中有几个MenuItem。 In a particular scenario, I need to place a checkmark beside one of the items. 在特定的情况下,我需要在其中一项旁打勾。

Is there any way to do this? 有什么办法吗?

I would settle for adding a Image control to the right of it if I had to. 如果需要的话,我想在其右侧添加一个Image控件。

I've even tried doing this: 我什至尝试这样做:

CheckBox box = new CheckBox();
menuItem.Items.Add(box);

but got a NotImplementedException. 但是收到了NotImplementedException。

Thanks! 谢谢!

you can add a listbox to contextmenu and then add checkbox to the listbox. 您可以将一个列表框添加到contextmenu,然后将复选框添加到该列表框。 I have not implemented but you can give it a try. 我尚未实施,但您可以尝试一下。

I found an easy way to get this working. 我找到了一种简单的方法来使其工作。 I bound the menu item's Header text property in xaml to a string property which adds the unicode checkmark character (0x2714) if the flag is set. 我在xaml中将菜单项的Header文本属性绑定到一个字符串属性,如果设置了标志,该属性将添加Unicode选中标记字符(0x2714)。

<toolkit:MenuItem 
      Header="{Binding InvertedCommandHeader}"              
      Click="onCommandInvert"
/>

I defined the dependency properties in my data context object like so: 我在数据上下文对象中定义了依赖项属性,如下所示:

public const string InvertedPropertyTag = "Inverted";
public static readonly DependencyProperty InvertedProperty =
    DependencyProperty.Register(
        InvertedPropertyTag,
        typeof(bool),
        typeof(MyData),
        new PropertyMetadata(false));
public bool Inverted
{
  get
  {
    return (bool)GetValue(InvertedProperty);
  }
  set
  {
    SetValue(InvertedProperty, value);

    InvertedCommandHeader = value ?
      Strings.IDST_MENUITEM_INVERTED + "  \u2714" :
      Strings.IDST_MENUITEM_INVERTED;
  }
}

public const string InvertedCommandHeaderPropertyTag = "InvertedCommandHeader";
public static readonly DependencyProperty InvertedCommandHeaderProperty =
    DependencyProperty.Register(
        InvertedCommandHeaderPropertyTag,
        typeof(string),
        typeof(MyData),
        new PropertyMetadata(
          Strings.IDST_MENUITEM_INVERTED));
public string InvertedCommandHeader
{
  get
  {
    return (string)GetValue(InvertedCommandHeaderProperty);
  }
  set
  {
    SetValue(InvertedCommandHeaderProperty, value);
  }
}

When the menu item is selected, I toggle the checkmark property. 选择菜单项时,我切换了复选标记属性。

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

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