我們在其子控件之一上有一個帶有上下文菜單的用戶控件。
命令綁定到ViewModel中的RelayCommand。
但是,該命令必須對View中的另一個子控件起作用。
這樣做的最好方法是什麼?我曾嘗試通過所需的子控件作爲參數,但我覺得語法不正確:SL4:將視圖中的元素作爲參數傳遞給ViewModel中的命令
<Controls:ContextMenu >
<Controls:MenuItem Header="Center" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click" >
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding RecenterCommand}"
CommandParameter="{Binding ElementName=scrollViewer}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Controls:MenuItem>
</Controls:ContextMenu>
命令:
RecenterCommand = new RelayCommand<ScrollViewer>(Recenter);
private void Recenter(ScrollViewer obj)
{
}
當我使用上下文菜單,Recenter()被調用,但obj參數爲null。
CommandParameter綁定中的ElementName屬性的正確語法是什麼?
更新:我試圖改變CommandParameter到:
CommandParameter="{Binding ElementName=LayoutRoot, Path=scrollViewer}"
...但仍然無法正常工作。
感謝任何見解....
是你的元素其實x:Name = scrollViewer? – ecathell
是的,它是:
Number8