WPF ContextMenu 在MVVM模式中綁定 Command及使用CommandParameter傳參

來源:互聯網
上載者:User

標籤:image   listbox   apr   檔案   理解   stack   ons   man   source   

原文:WPF ContextMenu 在MVVM模式中綁定 Command及使用CommandParameter傳參

ContextMenu無論定義在.cs或.xaml檔案中,都不繼承父級的DataContext,所以如果要綁定父級的DataContext,直接DataContext=“{Binding}”是行不通的

不能綁父級,但是能綁資源

第一步:定義一個中間類用來做資來源物件

 1 public class BindingProxy : Freezable 2     { 3         #region Overrides of Freezable 4  5         protected override Freezable CreateInstanceCore() 6         { 7             return new BindingProxy(); 8         } 9 10         #endregion11 12         public object Data13         {14             get { return (object)GetValue(DataProperty); }15             set { SetValue(DataProperty, value); }16         }17 18         public static readonly DependencyProperty DataProperty =19             DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));20     }

第二步:引用命名空間,在控制項中定義資源

1 <UserControl.Resources>2         <libBinding:BindingProxy x:Key="BindingProxy" Data="{Binding}"/>3     </UserControl.Resources>

第三步:綁定ContextMenu、MenuItem

(Button.Command 和 ContextMenu.IsOpen 的綁定部分可以不關注,這兩個綁定是用來控制ContextMenu開啟的)

 1 <Button Command="{Binding Customfold}"> 2             <Button.ContextMenu> 3                 <ContextMenu DataContext="{Binding Data,Source={StaticResource BindingProxy}}"   4                              ItemsSource="{Binding ItemModelCollection}" 5                              IsOpen="{Binding OpenCustomfold,Mode=OneWay}"> 6                     <ContextMenu.ItemContainerStyle> 7                         <Style TargetType="MenuItem"> 8                             <Setter Property="Header" Value="{Binding ...}"/> 9                             <Setter Property="Command" Value="{Binding ...}"/>10                             <Setter Property="CommandParameter" Value="{Binding ...}"/>11                         </Style>12                     </ContextMenu.ItemContainerStyle>13                 </ContextMenu>14             </Button.ContextMenu>15             <Image .../>16         </Button>

第四步:傳遞參數

ContextMenu是它自身視覺樹的根節點,所以即使通過RelativeSource.FindAncestor也找不到要傳遞的參數。

解決:可以通過PlacementTarget解決。微軟對PlacementTarget的解釋是:擷取或設定UIElement,當它開啟時相對於它確定ContextMenu的位置。應該可以理解為放置此ContextMenu的UIElement。

 1 CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget}"  

如果要傳遞Item,如ListBox的SelectedItem:

 1 CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"  

參考:

http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/

http://stackoverflow.com/questions/1580753/wpf-contextmenu-with-itemssource-how-to-bind-to-command-in-each-item

WPF ContextMenu 在MVVM模式中綁定 Command及使用CommandParameter傳參

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.