ViewModelのデータから階層的コンテキストメニューを動的に作成したいと思います。ビューモードで階層的コンテキストメニューを動的に作成するMVVM
、私はContextMenuAction定義:ビューで
public class ContextMenuAction : ViewModelBase
{
public string Header { get; set; }
public ICommand Action { get; set; }
public Brush Icon { get; set; }
public ObservableCollection<ContextMenuAction> SubActions { get; set; } = new ObservableCollection<ContextMenuAction>();
}
:
<ContextMenu ItemsSource="{Binding Path=PlacementTarget.Tag.Actions, RelativeSource={RelativeSource Self}}">
<ContextMenu.ItemTemplate >
<DataTemplate DataType="MenuItem">
<MenuItem/>
</DataTemplate>
</ContextMenu.ItemTemplate>
<ContextMenu.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="ItemsSource" Value="{Binding SubActions}"/>
<Setter Property="Header" Value="{Binding Header}" />
<Setter Property="Command" Value="{Binding Action}"/>
</Style>
</ContextMenu.ItemContainerStyle>
これが結果、コンテキストメニューでテキストなしです。
私はすでに結合をチェックするために、出力ウィンドウをチェックは、すべてのバインディングが動作し、例外はありません。
私はその理由を見つけるのを助けてください、事前に感謝!
実行時にのMenuItemのDataContextのを確認するために、「スヌープ」プログラムを使用して、データがあなたはそれがあることを期待するものであるかどうかを確認することができます。 "wpf snoop"についてgoogleを検索してください。最近オープンソースプログラムです。 – Mishka