我想将命令参数设置为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