Binding selected items in nested ListBox with Sele

2019-09-01 12:08发布

I'm developing a Windows Phone 8.1 app with MVVM pattern (I'm using Prism as a framework)

The goal is to get the selected items in a few lists, like that:

enter image description here

I have following XAML:

    <ListView 
    x:Name="abc"
    ItemsSource="{Binding Symbols}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <ListBox
                    SelectionMode="Multiple"
                    ItemsSource="{Binding List}"
Extensions:ListViewExtensions.BindableSelection="{Binding SelectedItems, ElementName=abc, Mode=TwoWay}">
                    ...

and in the ViewModel:

    public List<SymbolsGroupViewModel> Symbols {
        get { return _symbols ?? (_symbols = _dataService.Symbols.GetGroupViewModels()); }
        set { _symbols = value; }
    }

where class SymbolsGroupViewModel looks like that:

public class SymbolsGroupViewModel : ViewModel
{
    private readonly INavigationService _navigationService;
    private readonly DataService _dataService;

    private ObservableCollection<Symbol> _selectedItems = new ObservableCollection<Symbol>();

    public SymbolsGroupViewModel(INavigationService navigationService, DataService dataService)
    {
        _navigationService = navigationService;
        _dataService = dataService;
    }

    public SymbolsGroupViewModel() { }

    public Symbol Header { get; set; }

    public List<Symbol> List { get; set; }

    public ObservableCollection<Symbol> SelectedItems
    {
        get { return _selectedItems; }
        set { SetProperty(ref _selectedItems, value); }
    }
}

It is supposed to use the BindableSelection extension from WinRT XAML Toolkit, like here: https://stackoverflow.com/a/25430935/5194338

It works for me with NOT nested lists, however when I adapt the solution to nested list, the SelectedItems lists contain 0 elements.

Does anybody know if it is possible to use this extension in nested lists and if it is, what am I doing wrong?

Thank you for your help.

1条回答
混吃等死
2楼-- · 2019-09-01 12:36

I've found where the mistake was so I'll leave the answer in case someone had similar problem...

In the nested list the binding should have been written as below:

Extensions:ListBoxExtensions.BindableSelection="{Binding SelectedItems, Mode=TwoWay}"
  • Giving there ElementName was a bad idea
  • I haven't noticed before, that there are separate extensions for each kind of list. In my case I should have used ListBoxExtensions.

Hope this will help someone in the future.

查看更多
登录 后发表回答