問題描述
我有一個 MenuItem,其中包含一組項目.它看起來像文件 -> 打開菜單項.
I have a MenuItem, which has a collection of items in it. It looks like the File -> Open Menuitem.
所以:
- 文件
- 打開
- 從數據庫打開
- 文件 1
- 文件 2
- 文件 3
XAML 代碼:
<Menu> <MenuItem Header="File"> <MenuItem Header="Open"> <MenuItem Header="From Database" ItemsSource="{Binding OCFragebogen}"/> </MenuItem> </MenuItem> </Menu>
我想在單擊特定項目時調用命令.示例:用戶單擊文件 1,應調用命令,其中文件 1"是命令參數.
I want to call a Command, when a specific item has been clicked. Example: User clicks on File 1, a command should be called where the "File 1" is the Command Parameter.
ViewModel 包含我想在 MenuItem集合"中顯示的項目
ViewModel contains the Items, which I want to display in the MenuItem "collection"
private ObservableCollection<string> _OCFragebogen; public ObservableCollection<string> OCFragebogen { get { if (_OCFragebogen == null) _OCFragebogen = new ObservableCollection<string>(); return _OCFragebogen; } set { _OCFragebogen = value; RaisePropertyChanged(() => OCFragebogen); } }
明確地說:當用戶點擊 MenuItem 中的一個項目(來自 ItemsSource)時,應該調用一個命令,我想對點擊的項目執行某些操作.
To make it clear: When the user clicks on an item (from the ItemsSource) in the MenuItem, a Command should be called where I want to do something with the clicked Item.
我必須在哪里使用命令來調用 ViewModel 中的方法 (RelayCommand)?我希望在單擊 ItemsSource 中的一個項目時使用它 + 我想將單擊的項目傳遞給該方法.
Where do I have to use the command to call a method (RelayCommand) in my ViewModel? I want it to be used when an Item from the ItemsSource has been clicked + I want to pass the clicked item to the method.
推薦答案
這應該對你有用
<MenuItem Header="From Database" ItemsSource="{Binding YourItemSource}"> <MenuItem.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.YourCommandName}"></Setter> <Setter Property="CommandParameter" Value="{Binding}"></Setter> </Style> </MenuItem.ItemContainerStyle> </MenuItem>
這篇關于將項目綁定到 MenuItem ->使用命令的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
- 從數據庫打開
- 打開