简体   繁体   中英

WP8 LongListMultiSelector Binding SelectedItems

I have a question concerning the LongListMultiSelector in the Windows Phone 8 Toolkit.

I want to use this control to implement a file browser in WP8 (using MVVM). Since the SelectedItems property is not bindable, I used the solution in this article to fix that. http://dotnet-redzone.blogspot.de/2012/11/windows-phone-8longlistselector.html

Here's my relevant code: XAML

 <Grid DataContext="{Binding FileBrowserViewModel}">
    <local:LongListMultiSelector
                    x:Name="FileList"
                    ItemsSource ="{Binding CurrentFileList}"
                    EnforceIsSelectionEnabled="{Binding IsInSelectionMode}" 
                    toolkit:TiltEffect.IsTiltEnabled="True" 
                    SelectedItems="{Binding SelectedFiles, Mode=TwoWay}"
                    IsSelectionEnabled="True"/>

 </Grid>

My LonglistMultiSelector

public class LongListMultiSelector : Microsoft.Phone.Controls.LongListMultiSelector
{  
    public LongListMultiSelector()
    {
        SelectionChanged += LongListMultiSelector_SelectionChanged;
    }

    void LongListMultiSelector_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        this.SelectedItems = base.SelectedItems;
    }

    public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.Register(
            "SelectedItems",
            typeof(object),
            typeof(LongListMultiSelector),
            new PropertyMetadata(null, OnSelectedItemsChanged)
        );

    private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var selector = (LongListMultiSelector) d;
        selector.SelectedItems = e.NewValue;
    }

    public new object SelectedItems
    {
        get { return GetValue(SelectedItemsProperty); }
        set { SetValue(SelectedItemsProperty, value); }
    }
}

VIEW MODEL

/// <summary>
/// The currently selected Items.
/// </summary>
public ObservableCollection<File> SelectedFiles
{
    get { return _selectedFiles; }
    set { Set(() => this.SelectedFiles, ref _selectedFiles, value); }
}
private ObservableCollection<File> _selectedFiles;

But this solution does not work. The SelectedFiles Property does not change at all. (_selectedFiles is always null)

  • Edit: Set(() => this.SelectedFiles, ref _selectedFiles, value); is from the Mvvmlight (Laurent Bugnion) package.

I solved my problem with using a normal LongListSelector and giving each Item inside it a Boolean IsSelected.

The DataTemplate then has a checkbox that looks like this:

<CheckBox IsChecked="{Binding IsSelected, Converter={StaticResource BooleanToVisibilityConverter}}"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM