2013-12-13 17 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時,沒有任何反應,因爲選擇不會改變。我需要能夠在兩種情況下檢測項目點擊。 –

相關問題