問(wèn)題描述
我有一個(gè) MenuItem,其中包含一組項(xiàng)目.它看起來(lái)像文件 -> 打開(kāi)菜單項(xiàng).
I have a MenuItem, which has a collection of items in it. It looks like the File -> Open Menuitem.
所以:
- 文件
- 打開(kāi)
- 從數(shù)據(jù)庫(kù)打開(kāi)
- 文件 1
- 文件 2
- 文件 3
XAML 代碼:
<Menu> <MenuItem Header="File"> <MenuItem Header="Open"> <MenuItem Header="From Database" ItemsSource="{Binding OCFragebogen}"/> </MenuItem> </MenuItem> </Menu>
我想在單擊特定項(xiàng)目時(shí)調(diào)用命令.示例:用戶單擊文件 1,應(yīng)調(diào)用命令,其中文件 1"是命令參數(shù).
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集合"中顯示的項(xiàng)目
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); } }
明確地說(shuō):當(dāng)用戶點(diǎn)擊 MenuItem 中的一個(gè)項(xiàng)目(來(lái)自 ItemsSource)時(shí),應(yīng)該調(diào)用一個(gè)命令,我想對(duì)點(diǎn)擊的項(xiàng)目執(zhí)行某些操作.
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.
我必須在哪里使用命令來(lái)調(diào)用 ViewModel 中的方法 (RelayCommand)?我希望在單擊 ItemsSource 中的一個(gè)項(xiàng)目時(shí)使用它 + 我想將單擊的項(xiàng)目傳遞給該方法.
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.
推薦答案
這應(yīng)該對(duì)你有用
<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>
這篇關(guān)于將項(xiàng)目綁定到 MenuItem ->使用命令的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!
- 從數(shù)據(jù)庫(kù)打開(kāi)
- 打開(kāi)