2013-12-13 18 views
2

我想将命令参数设置为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时,什么都不会发生,因为电子选择不会改变。我需要能够在两种情况下检测项目点击。

+0

'Places'类型是'BasicModel'? – ebattulga

+0

是的,Places是一个'ObservableCollection ' –

+0

请解释(在你的问题的编辑)究竟你的要求实际上是什么。忘记'ICommand's和'SelectedItem's一分钟,并告诉我们你究竟在尝试用这个来实现。 – Sheridan

回答

0

我想出了一个难以实现的目标。

XAML

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedPlace, Mode=TwoWay}"> 

C#(视图模型)

private bool _placeSelected; 

public BasicModel SelectedPlace { 
    get { 
     return _selectedPlace; 
    } 
    set { 
     _placeSelected = true; 
     _selectedPlace = value; 
     RaisePropertyChanged("SelectedPlace"); 
    } 
} 

ListBoxClick = new RelayCommand((o) => { 
    if (!_placeSelected) { 
     SelectedPlace = _selectedPlace; 
    } 
    else { 
     _placeSelected = false; 
    } 
}); 

这样RaisePropertyChanged("SelectedPlace");会在两种情况下被调用。

0

试试这个:

<ListBox ItemsSource="{Binding Places}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Tap"> 
      <i:InvokeCommandAction Command="{Binding ListBoxClick}" 
CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource AncestorType={ 
x:Type ListBox}}}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      (...) 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

如果还是不行,请尝试更改Binding这样:

CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}" 

UPDATE >>>

噢,对不起,我没有看到您使用的是Windows Phone 7.作为替代方案,请尝试将代码添加到代码背后/ view模型中NDS的ListBox.SelectedItem属性:

<ListBox ItemsSource="{Binding Places}" SelectedItem="{Binding SelectedItem}" ... /> 

那么你应该能够做到这一点:

ListBoxClick = new RelayCommand(() => { 
    SelectedPlace = SelectedItem; 
}); 

更新2 >>>

我不知道的Windows Phone 7支持Binding.ElementName属性,但如果有,请尝试以下操作:

<ListBox Name="ListBox" ItemsSource="{Binding Places}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="Tap"> 
      <i:InvokeCommandAction Command="{Binding ListBoxClick}" 
CommandParameter="{Binding SelectedItem, ElementNameListBox}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      (...) 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

当我使用第二种解决方案时,“o”仍然为空,当我使用第一个解决方案时,我得到:'属性'AncestorType'不存在于XML名称空间'http:// schemas中的'RelativeSource'类型中。 microsoft.com/winfx/2006/xaml/presentation'','找不到类型'x:Type'。验证你是否遗漏了一个程序集引用,并且所有引用的程序集都已经构建好了。',''RelativeSource'.'类型中找不到属性'AncestorType'。我在这个视图中使用的命名空间如下: –

+0

'xmlns =“http://schemas.microsoft.com/winfx/2006/xaml/presentation”' 'xmlns:x =“http://schemas.microsoft.com/winfx/2006/xaml“' 'xmlns:d =”http://schemas.microsoft.com/expression/blend/2008“' 'xmlns:mc =”http://schemas.openxmlformats.org/markup -compatibility/2006“' 'xmlns:i =”clr-namespace:System.Windows.Interactivity; assembly = System.Windows.Interactivity“' –

+0

我确实有这个属性,它被称为SelectedPlace :)。也许我应该解释为什么我使用触发器而不是使用SelectedItem属性。随着后来的一切工作正常,如果我第一次点击ListBoxItem,但是当我点击一个选定的ListBoxItem时,没有任何反应,因为选择不会改变。我需要能够在两种情况下检测项目点击。 –

相关问题