【问题标题】:WPF Button with Context Menu带有上下文菜单的 WPF 按钮
【发布时间】:2013-12-23 14:42:37
【问题描述】:

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

这就是我正在做的:

<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;                
  }
}

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

我在这里做错了什么?

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

【问题讨论】:

  • 请问有什么想法吗?

标签: wpf button contextmenu


【解决方案1】:

如果您执行ItemsSource="ItemsList",则不会绑定到ItemsList,而是将其设置为字符串ItemsList,因此会出现错误。尝试像这样绑定它:

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

至于Command 部分,您需要一些ICommand 接口的实现(如here),然后像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 >

【讨论】:

  • 谢谢 dkozl。我现在可以得到数据了。但由于某种原因,XAML 一直说“命令”不是我可以设置的属性。我想我在做一些愚蠢的事情,但不确定是什么。
  • 试试&lt;Style TargetType="{x:Type MenuItem}"&gt;
  • 虽然它现在没有抛出任何错误,但是命令没有被触发。也许我们不应该在这里使用“ItemContainerStyle”?
  • 这里是你绑定Command的地方。您的命令(您绑定的命令)在哪里定义?它是定义ItemsList 的类的另一个属性吗?
  • 是的。我在视图模型中将它作为 ICommand 属性,如下所示(我使用 MVVM Light 作为 RelayCommand 的东西): public ICommand ItemChanged { get;放; } //在构造函数中 ItemChanged = new RelayCommand(ChangeItem); public void ChangeItem() { MessageBox.Show("Item Changed"); }
【解决方案2】:

xaml

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

xaml.cs

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

视图模型

    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;

}

我希望这会有所帮助。

【讨论】:

  • 为什么我不能从 xaml.cs 按名称访问ContextMenu
猜你喜欢
  • 1970-01-01
  • 2018-09-16
  • 2013-02-20
  • 2013-11-09
  • 1970-01-01
  • 2013-07-06
  • 2018-07-27
  • 2014-05-04
  • 2022-12-31
相关资源
最近更新 更多