2012-03-16 73 views
1

我不能讓我的綁定工作。我的ActiveView有一個ListView(x:Name =「MyListView」)以及視圖模型,因爲它是數據上下文。WPF DataBinding(CommandParameter不工作)

我有以下RibbonButton(RibbonControlLibrary):

<r:RibbonButton Label="Update Software" 
    LargeImageSource="/Ganymed.App.UI;component/Images/plain-update.png" 
    Command="{Binding ActiveView.DataContext.UpdateSoftwareCommand}" 
    CommandParameter="{Binding ActiveView.MyListView.SelectedItems}"/> 

命令結合自身工作正常。但是我無法使CommandParameter工作。我想要沿着ActiveView的ListView的選定項目發送,以決定是否啓用該命令。綁定CommandParameter時是否缺少某些東西?

爲了闡明藥結構:

MainWindow.xaml(包含RibbonButton,DataContext的= MainWindowViewModel)

MainWindowViewModel.cs包含屬性的ActiveView

的ActiveView(包含MyListView,DataContext的= ActiveViewViewModel)

ActiveViewViewModel.cs包含UpdateSoftwareCommand

任何幫助都會很棒。由於

+0

應該不會吧蜜蜂ActiveView.MyListView而不是ActiveView.ListView,因爲你說名稱是「MyListView」? – SvenG 2012-03-16 15:09:36

+0

嗨,這是我複製代碼時的錯誤。我在示例中更改了名稱,以免混淆其他人。 – 2012-03-16 15:14:21

+0

您的UpdateSoftwareCommand代碼的外觀如何?如果您使用DelegateCommand,那麼您應該使用DelegateCommand 版本 – Nikolay 2012-03-16 15:24:43

回答

0

一種可能性:

CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type r:RibbonButton}}, Path=SelectedItems}" 

另外,你可以只添加屬性SelectedItems你的視圖模型,將其綁定並使其始終保持同步,並準備命令進行操作

+0

您的解決方案無法正常工作。我猜這是行不通的,因爲ListView並不是Visual Tree中Button的祖先。 – 2012-03-19 07:06:58

+0

對不起,我的意思是把你的ViewModel綁定爲ancenstor – 2012-03-21 22:29:52