2015-12-08 56 views
0

我試圖訪問SelectedItem以及命令綁定到ItemsControl中的嵌套列表框。以下是簡化的XAML。如何獲取對嵌套列表框中的SelectedItem的訪問

<ItemsControl ItemsSource="{Binding Collection}" 
    ItemTemplate="{StaticResource Partials}"        
</ItemsControl> 

<DataTemplate x:Key="Partials"> 
    <ListBox ItemsSource="{Binding ListBoxItems}" 
      SelectedItem="{Binding SelectedItem}" > 

     <ListBox.InputBindings> 
      <KeyBinding Key="Delete" Command="{Binding DeleteSelectedItemCommand/> 
     </ListBox.InputBindings> 
    </ListBox> 
</DataTemplate> 

這是我在VM(簡體)

private void FillList() 
{ 
    //Populate the list 
    Collection.Add(Data); 

    var ListBoxViewSource = new CollectionViewSource { Source = ListBoxDataSource}; 
    Collection.ListBoxItems= ListBoxViewSource.View; 
} 

private ObservableCollection<Scene> _collection= new ObservableCollection<Scene>(); 
public ObservableCollection<Scene> Collection 
{ 
    get { return _collection; } 
    set 
    { 
     if (value != _collection) 
     { 
      _collection= value; RaisePropertyChanged(); 
     } 
    } 
} 

private string _selectedItem; 
public string SelectedItem 
{ 
    get { return _selectedItem; } 
    set { _selectedItem= value; RaisePropertyChanged(); } 
} 

public RelayCommand DeleteSelectedItemCommand { get; private set; } 
private void DeleteSelectedItem() 
{ 
    SourceData.Remove(SelectedItem); 
} 

怎樣才能獲得ListBox中的SelectedItem的價值?

+0

您是否在輸出窗口中收到任何綁定錯誤? – user1672994

+0

我認爲不需要像這樣聲明一個DataTemplate。 –

+0

你的問題不清楚。請提供一個很好的[mcve],它可以可靠地再現您遇到的任何問題,並精確描述該代碼的功能以及您希望它執行的操作。詢問如何讓綁定工作並不會有幫助,除非您確切地說明他們在「工作」時會做什麼。 –

回答

0
  1. 要獲得SelectedItem,必須在DataContext類中使用一個屬性。

  2. 您使用ListBoxes作爲項目,因此您必須在內部DataContext中聲明一個屬性。

  3. 您還可以在ItemsControl級別使用RoutedEvent(ListBox.SelectionChanged)從ListBox中獲取SelectedValue。

樣品:

視圖模型

public class StateVM 
    {   
     public List<CountryStates> Collection { get; set; } 
     public StateVM() { Collection = new List<CountryStates>(); } 
    } 

public class CountryStates 
    { 
     public string SelectedState { get; set; } /* SelectedItem will arrive here */ 

     public string Name { get; set; } 
     public List<string> States { get; set; } 
    } 

XAML

<DataTemplate x:Key="Partials"> 
    <ListBox ItemsSource="{Binding States}" SelectedValue="{Binding SelectedState}"> 
     <ListBox.InputBindings> 
      <KeyBinding Key="Delete" Command="{Binding DeleteSelectedItemCommand}"/> 
     </ListBox.InputBindings> 
    </ListBox> 
</DataTemplate> 

<ItemsControl x:Name="CountryStateList" ItemsSource="{Binding Collection}" 
       ListBox.SelectionChanged="ListBox_SelectionChanged" 
       ItemTemplate="{StaticResource Partials}"> 
</ItemsControl> 

代碼隱藏

StateVM vm = new StateVM(); 
vm.Collection.Add(new CountryStates() { Name = "India", States = new[] { "mp", "up", "tn" }.ToList() }); 
vm.Collection.Add(new CountryStates() { Name = "USA", States = new[] { "new york", "washington" }.ToList() }); 

CountryStateList.DataContext = vm; 

...

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ListBox source = (ListBox)(e.OriginalSource); 
    System.Diagnostics.Debug.WriteLine(">>>>>> " + ((CountryStates)(source.DataContext)).SelectedState); 
} 
相關問題