WPF MVVM动态子菜单绑定问题

WPF MVVM动态子菜单绑定问题,wpf,mvvm,submenu,Wpf,Mvvm,Submenu,我正在动态创建一个上下文菜单,菜单项有子项。 第一次出现在子菜单周围,但第二次出现在子菜单上,之后仅显示父菜单。子菜单位于绑定到上下文菜单的集合中,但它们不显示 VMMenuItems是我的视图模型中的一个属性,是 ObservableCollection<MenuItemViewModel> observedcollection 每次Listview中的数据更改时,VMMenuItems都会完全重建。 子菜单只是将另一个MenuItemViewModel添加到现有MenuIte

我正在动态创建一个上下文菜单,菜单项有子项。 第一次出现在子菜单周围,但第二次出现在子菜单上,之后仅显示父菜单。子菜单位于绑定到上下文菜单的集合中,但它们不显示

VMMenuItems是我的视图模型中的一个属性,是

ObservableCollection<MenuItemViewModel>
observedcollection
每次Listview中的数据更改时,VMMenuItems都会完全重建。 子菜单只是将另一个MenuItemViewModel添加到现有MenuItemViewModel的子菜单中。 关于如何使子菜单每次都工作,有什么想法吗

代码

<Window.Resources>        
    <HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
                              ItemsSource="{Binding Path=Children}">
        <ContentPresenter Content="{Binding Path=MenuText}" />
    </HierarchicalDataTemplate>
</Window.Resources>

 <ListView.ContextMenu>
       <ContextMenu ItemsSource="{Binding Path=VMMenuItems>
          <ContextMenu.ItemContainerStyle>
              <Style TargetType="{x:Type MenuItem}">                                    
                 <Setter Property="Command" Value="{Binding Command}"/>
                  <Setter Property="CommandParameter" Value="{Binding MenuText}"/> 
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>                       
                </ListView.ContextMenu>

public class MenuItemViewModel : ViewModel
{  
    public MenuItemViewModel()
    {
        Children = new ObservableCollection<MenuItemViewModel>();
    }


    private string _menuText;
    public string MenuText
    {

        get { return _menuText; }

        set
        {
            _menuText = value;
            OnPropertyChanged("MenuText");
        }
    }


    private bool _isEnabled;
    public bool IsEnabled
    {
        get { return _isEnabled; }

        set
        {
            _isEnabled = value;
            OnPropertyChanged("IsEnabled");
        }
    }


    private ICommand _command;
    public ICommand Command
    {
        get { return _command; }

        set
        {
            _command = value;
            OnPropertyChanged("Command");
        }
    }

    private ObservableCollection<MenuItemViewModel> _children;
    public ObservableCollection<MenuItemViewModel> Children
    {
        get { return _children; }

        set
        {
            _children = value;
            OnPropertyChanged("Children");
        }
    }


我自己还是个新手,如果不进行测试,我也不知道确切原因,但我相信这与用全新的系列取代儿童系列有关。我认为这需要重新绑定集合。最好从现有集合中添加/删除项目。这将触发相应的绑定通知。现在,绑定到集合的特定实例,该实例将在Children.set调用中被替换。希望这能有所帮助。

我没有使用HierarchycalDataTemplate,而是将其全部放在ContextMenu.ItemContainerStyle中。 我不知道为什么我的另一种方法不起作用(第一次它起作用了,但其他方法都不起作用)。 也许其他人可以告诉我为什么它不起作用

<ContextMenu.ItemContainerStyle>
  <Style TargetType="{x:Type MenuItem}">
   <Setter Property="Header" Value="{Binding MenuText}"/>
   <Setter Property="ItemsSource" Value="{Binding Path=Children}"/>
   <Setter Property="Command" Value="{Binding Command}"/>
   <Setter Property="CommandParameter" Value="{Binding MenuText}"/>
  </Style>
 </ContextMenu.ItemContainerStyle>


噢,如果您需要设置整个集合,我认为您需要处理视图代码隐藏中的子属性的PropertyChanged通知来执行重新绑定。我从集合和子集合中删除了所有项,并重新添加了新的MenuItemViewModel对象,但仍然得到相同的结果,第二次没有子菜单。就像当属性更改时,它只看到顶层更改,而不看到子菜单/子菜单。有没有办法强迫它递归地遍历所有集合?