【问题标题】:How to get access to SelectedItem in Nested Listbox如何访问嵌套列表框中的 SelectedItem
【发布时间】:2015-12-08 23:34:59
【问题描述】:

我正在尝试访问 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>

这就是我在 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);
}

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

【问题讨论】:

  • 您是否在输出窗口中遇到任何绑定错误?
  • 我认为没有必要像这样声明一个DataTemplate。
  • 您的问题不清楚。请提供一个好的minimal reproducible example,它可以可靠地重现您遇到的任何问题,并准确描述该代码的作用以及您希望它做什么。询问如何使绑定发挥作用是没有帮助的,除非你确切地说出它们在“工作”时会做什么。

标签: c# wpf mvvm binding listbox


【解决方案1】:
  1. 要获取 SelectedItem,您必须使用 DataContext 类中的属性。

  2. 您将 ListBox 用作项目,因此您必须在内部 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);
}

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 2021-04-02
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 2021-01-08
    • 2017-10-26
    • 1970-01-01
    • 2017-07-03
    相关资源
    最近更新 更多