我可以帮您翻译。以下是翻译结果:
问题在于由
当我使用这个时:
我想将命令参数设置为当前选定项的ListBox。
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#(暴露ListBoxClick命令的ViewModel代码部分)
public RelayCommand ListBoxClick { get; set; }
ListBoxClick = new RelayCommand((o) => {
//model is always null
var model = o as BasicModel;
SelectedPlace = model;
});
我添加了适当的引用和命名空间:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
问题在于由
RelayCommand
对象调用的操作中,o
参数始终为空。
更新
SelectedPlace
属性的C#代码。public BasicModel SelectedPlace {
get {
return _selectedPlace;
}
set {
_selectedPlace = value;
RaisePropertyChanged("SelectedPlace");
}
}
当我使用这个时:
<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}">
首次单击 ListBoxItem 时,所有内容都正常工作,但是当我单击所选的 ListBoxItem 时,什么也不会发生,因为选择并未更改。我需要能够在这两种情况下检测到项目点击。
Places
类型是BasicModel
吗? - undefinedObservableCollection<BasicModel>
。 - undefinedICommand
和SelectedItem
,告诉我们你实际上想通过这个实现什么目标。 - undefined