久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用父級的 DataContext(WPF - 動態(tài)菜單命令綁定)

Using the parent#39;s DataContext (WPF - Dynamic Menu Command Binding)(使用父級的 DataContext(WPF - 動態(tài)菜單命令綁定))
本文介紹了使用父級的 DataContext(WPF - 動態(tài)菜單命令綁定)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我瀏覽了這個網(wǎng)站和谷歌,但這些解決方案對我不起作用.

I looked over this web and google and the solutions didn't work for me.

我在 UserControl 的 ViewModel 上有一個命令.好吧,用戶控件有一個綁定到 ObservableCollection 的 ItemsControl.在 ItemsControl.ItemTemplate 的 DataTemplate 中,我有一個按鈕,我想使用該命令.我無法綁定命令,因為在 DataTemplate 內(nèi)部,數(shù)據(jù)上下文不是 ViewModel,而是 ObservableCollection 的一個項目.

I have a command on the ViewModel of a UserControl. Well, The usercontrol have a ItemsControl binded to a ObservableCollection. Inside the DataTemplate of the ItemsControl.ItemTemplate I have a button and I want to use the command. I can't bind the command because inside the DataTemplate, the datacontext is not the ViewModel but an item of the ObservableCollection.

問題是:如果丟失了父數(shù)據(jù)上下文,我如何將按鈕綁定到命令?

The question is: How can I bind the button to the command if a lost the parent datacontext?

我認為這需要一個簡單的解決方案,因為我認為這是一個常見問題.

I think that this need to have an easy solution because I think that this is a common problem.

想象一下這個場景:

您有一個以 observableCollection 作為 ItemsSource 的 ListBox 項目,因此您在 ListBox 內(nèi)為集合中的每個元素使用了一個數(shù)據(jù)模板.好吧,您想刪除所選項目,并在該作業(yè)的每一行中放置一個按鈕.?你是怎么做到的?

You have a ListBox item with an observableCollection as the ItemsSource, so you are using a datatemplate inside the ListBox for every element in the collection. Well, you want to delete the selected item and you put a button in every row for that job. ?How do you do that?

在MVP中,我可以在按鈕的點擊事件中做到這一點:

In MVP, I can do this in the click event of the button:

Button but = e.Source as Button;

if (but != null)
      Presenter.ActualNote = but.DataContext as Note;

簡而言之.您將行(所選項目)的數(shù)據(jù)上下文發(fā)送給演示者.

In short. You send the datacontext of the row (the selected item) to the presenter.

但是,我怎樣才能以 mvvm 的方式做到這一點?因為我需要使用命令但我無法將命令分配給按鈕,因為按鈕對 ViewModel(命令所在的位置)一無所知.

But, how can I do it in the mvvm way? Because I need to use a command but I can't assign the command to the button because the button does know nothing about the ViewModel (where the command exists).

如您所見,按鈕必須存在于數(shù)據(jù)模板中,然后數(shù)據(jù)上下文不再是 ViewModel ......這就是為什么我需要訪問父級的 DataContext,以訪問命令.

As you can see, the button has to exist inside the datatemplate, then the datacontext is not the ViewModel anymore.... There is why I need to access to the parent's DataContext, for access to the command.

我希望你能更好地理解我的問題.

I hope that you understand my problem better.

謝謝.

推薦答案

如果你想要一個骯臟的、破壞 MVVM 的解決方案,那么在按鈕上設置 Tag="{Binding}" 并處理 Click 事件.在事件處理程序中,調(diào)用 ViewModel 上的命令.

If you want a dirty, MVVM-breaking solution, then set the Tag="{Binding}" on the button and handle the Click event. In the event handler, call the command on your ViewModel.

這篇關(guān)于使用父級的 DataContext(WPF - 動態(tài)菜單命令綁定)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
Removing thin border around the menuitems(刪除菜單項周圍的細邊框)
主站蜘蛛池模板: 日韩欧美在线观看 | 欧美色视频免费 | 欧美激情国产日韩精品一区18 | 国产精品视频综合 | 污污免费网站 | 久久国产欧美日韩精品 | 色约约视频 | 99国产精品99久久久久久 | 久久性av| 婷婷色网 | 黄网站在线播放 | 国产精品欧美一区二区三区不卡 | 亚洲精品福利视频 | 成人日b视频 | 久久伊人影院 | 欧美三级网站 | 91中文字幕在线 | 射欧美| 婷婷中文字幕 | 欧美一级在线 | a在线观看 | h视频免费观看 | 精品国产91| 精品国产免费一区二区三区五区 | 亚洲人成人一区二区在线观看 | 天天射美女 | 午夜精品久久久久久久久久久久 | 日韩一区二区三区精品 | 日韩毛片免费视频 | 久久艹av| 91电影院| 欧美精品国产精品 | 国产成人高清成人av片在线看 | a精品视频| 国产精品免费播放 | 九九热在线视频 | www.色婷婷 | 中文字幕日韩一区 | 日韩一区二区在线视频 | 亚洲欧美视频一区二区 | 亚洲欧洲精品在线 |