2014-07-16 95 views
1

之外我有AppBarButton與命令綁定到視圖模型 - 內部命令欄(WinRT窗口8.1):綁定CommandParameter到UI元素命令欄

<CommandBar.PrimaryCommands> 
    <AppBarButton Name="someOtherButton" ../> 
    <AppBarButton Name="CropImageButton" Icon="Crop" Label="Crop image" 
       Command="{Binding CropImageCommand}" CommandParameter="{Binding ElementName=croppableImage, Path=OrginalSelectedArea}" /> 
</CommandBar.PrimaryCommands>    

不幸的是,這並不工作 - 參數始終是零。然而,這種結合的作品:

CommandParameter={Binding ElementName=someOtherButton, ..} 

我固定,通過在視圖模型創建SelectedArea屬性,而不是使用CommandParameter但我很好奇的綁定ElementName通過某種方式限制範圍?

回答

1

我也遇到過這個問題。將UI元素綁定到命令參數始終爲空。這可能是元素在執行綁定時還沒有準備好。

1

如果您沒有明確設置CommandBar.DataContext而不是使用ElementName作爲綁定的源,那麼似乎與CommandBar的綁定無法正常工作。

編輯:這是WinRT 8.1平臺上的一個錯誤。 DataContext不會正確傳播給所有頁面的孩子,現在這個問題已經解決並且有效。

+0

我在VS2015.2中得到了與Windows10開發相同的錯誤情況。我不知道這個錯誤是否被固定爲8.1,它是在Win10上的! –

+0

我現在在我的應用程序中使用這個,我很確定它在10上工作。 沒有代碼,沒有ElementName,只是普通的綁定。也許還有一些問題與「時機」雖然,誰知道? (例如*當*和*在哪裏設置DataContext?在我的情況下,我在瀏覽頁面,在OnNavigatedTo方法中設置它,並且所有工作都按預期工作)。 –

+0

感謝@Tommaso,或許是時候分享一些片段了?我使用Prism MVVM的「AutowireViewModel = True」方法將DataContext綁定到視圖。肯定有一些綁定正在進行:在我的ViewModel中,我收到Button作爲參數,但CommandParameter仍爲空。綁定其他按鈕並從那裏設置和接收CommandParameters沒有問題。無論如何,我在AppBarButton Click eventhandler上創建了一行代碼隱藏的解決方法:DataContext.CommandDoMyThing.Execute(ElementName)。 – user2366741