我想將命令參數設置爲ListBox上當前選定的項目。將CommandParameter設置爲當前點擊列表框上的項目(WP7)
XAML:
<!--<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">-->
<ListBox ItemsSource="{Binding Places}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<i:InvokeCommandAction Command="{Binding ListBoxClick}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<ListBox.ItemTemplate>
<DataTemplate>
(...)
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#
public RelayCommand ListBoxClick { get; set; }
ListBoxClick = new RelayCommand((o) => {
//model is always null
var model = o as BasicModel;
SelectedPlace = model;
});
我加入適當的引用,和命名空間(的視圖模型代碼暴露ListBoxClick命令部分):
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
問題在於,在RelayCommand
對象所調用的對象中,參數o
始終爲空。
UPDATE
C#代碼SelectedPlace
財產
public BasicModel SelectedPlace {
get {
return _selectedPlace;
}
set {
_selectedPlace = value;
RaisePropertyChanged("SelectedPlace");
}
}
當我使用這個:如果我點擊ListBoxItem的首次
<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">
一切工作正常,但當我點擊一個選定的ListBoxItem時,什麼都不會發生,因爲電子選擇不會改變。我需要能夠在兩種情況下檢測項目點擊。
'Places'類型是'BasicModel'? – ebattulga
是的,Places是一個'ObservableCollection' –
請解釋(在你的問題的編輯)究竟你的要求實際上是什麼。忘記'ICommand's和'SelectedItem's一分鐘,並告訴我們你究竟在嘗試用這個來實現。 – Sheridan