如何在嵌套列表框中访问SelectedItem

时间:2015-12-08 23:35:00

标签: c# wpf mvvm binding listbox

我正在尝试访问SelectedItem以及绑定到ItemsControl内嵌套ListBox的命令。下面是简化的XAML。

<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>

这就是我在虚拟机中所拥有的(简化)

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);
}

如何在列表框中获取SelectedItem的值?

1 个答案:

答案 0 :(得分:0)

  1. 要获取SelectedItem,您必须在DataContext类中使用属​​性。

  2. 您正在使用ListBoxes作为项目,因此您必须在内部DataContext中声明一个属性。

  3. 您还可以在ItemsControl级别使用RoutedEvent(ListBox.SelectionChanged)从ListBox获取SelectedValue。

  4. 示例:

    视图模型

    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);
    }
    
相关问题