带有上下文菜单的 WPF 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20745758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:17:02  来源:igfitidea点击:

WPF Button with Context Menu

wpfbuttoncontextmenu

提问by Padmaja

I am new to WPF and am trying to bind a Context Menu to a Button with the Context Menu items coming from a View Model.

我是 WPF 的新手,正在尝试将上下文菜单绑定到带有来自视图模型的上下文菜单项的按钮。

This is what I am doing:

这就是我正在做的:

<Button x:Name="btn" Content="Context Menu">
  <Button.ContextMenu>
    <ContextMenu x:Name="cm" ItemsSource="ItemsList"/>
  </Button.ContextMenu>
</Button>

private List<string> itemsList = null;
public List<string> ItemsList
{
  get
  {
    if(itemsList == null)
      itemsList = new List<string>(myStringArrayOfItems);
    return itemsList;                
  }
}

The XAML Editor keeps showing the error: The TypeConverter for "IEnumerable" does not support converting from a string.

XAML 编辑器不断显示错误:“IEnumerable”的 TypeConverter 不支持从字符串转换。

What am I doing wrong here?

我在这里做错了什么?

Also, assuming I get this working, what do I do to bind these items to a command and do some work when the item is clicked? I want to run the same command for all the menu items, just using the item string as a parameter.

另外,假设我得到了这个工作,我该怎么做才能将这些项目绑定到命令并在单击项目时做一些工作?我想对所有菜单项运行相同的命令,只需使用项字符串作为参数。

回答by dkozl

If you do ItemsSource="ItemsList"you don't bind to ItemsListbut set it to string ItemsList, hence your error. Try binding it like so:

如果你这样做,ItemsSource="ItemsList"你不会绑定到ItemsList而是将它设置为 string ItemsList,因此你的错误。尝试像这样绑定它:

<ContextMenu x:Name="cm" ItemsSource="{Binding Path=ItemsList}"/>

as for the Commandpart you'll need some implementation of ICommandinterface (like here) and then you bind it like in ItemContainerStyle:

至于Command部分,您将需要一些ICommand接口的实现(如此),然后您将其绑定如下ItemContainerStyle

<ContextMenu ...>
   <ContextMenu.ItemContainerStyle>
      <Style TargetType="{x:Type MenuItem}">
         <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacemantTarget.DataContext.ItemChanged }"/>
         <Setter Property="CommandParameter" Value="{Binding}"/>
      </Style>
   </ContextMenu.ItemContainerStyle>
</ContextMenu >

回答by yo chauhan

xaml

xml

<Button  Content="0k">
        <Button.ContextMenu>
            <ContextMenu x:Name="cm" ItemsSource="{Binding ItemsList}" />
        </Button.ContextMenu>
    </Button>

xaml.cs

xml文件

public MainWindow()
    {
        InitializeComponent();
        DataContext = new MyViewModel();
    }

ViewModel

视图模型

    public class MyViewModel : INotifyPropertyChanged
{

    public MyViewModel()
    {
        ItemsList = new List<string> { "abc", "xyz" };
    }

    private List<string> itemsList = null;

    public List<string> ItemsList
    {
        get
        {
            return itemsList;
        }
        set
        {

            if (itemsList == null)
            {
                itemsList = value;
                Notify("ItemsList");
            }

        }
    }

    private void Notify(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

I hope this will help.

我希望这将有所帮助。