之外我有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
通過某種方式限制範圍?
我在VS2015.2中得到了與Windows10開發相同的錯誤情況。我不知道這個錯誤是否被固定爲8.1,它是在Win10上的! –
我現在在我的應用程序中使用這個,我很確定它在10上工作。 沒有代碼,沒有ElementName,只是普通的綁定。也許還有一些問題與「時機」雖然,誰知道? (例如*當*和*在哪裏設置DataContext?在我的情況下,我在瀏覽頁面,在OnNavigatedTo方法中設置它,並且所有工作都按預期工作)。 –
感謝@Tommaso,或許是時候分享一些片段了?我使用Prism MVVM的「AutowireViewModel = True」方法將DataContext綁定到視圖。肯定有一些綁定正在進行:在我的ViewModel中,我收到Button作爲參數,但CommandParameter仍爲空。綁定其他按鈕並從那裏設置和接收CommandParameters沒有問題。無論如何,我在AppBarButton Click eventhandler上創建了一行代碼隱藏的解決方法:DataContext.CommandDoMyThing.Execute(ElementName)。 – user2366741