我对 Windows Phone 8 Toolkit 中的 LongListMultiSelector 有疑问。
我想用这个控件在 WP8 中实现一个文件浏览器(使用 MVVM)。由于 SelectedItems 属性不可绑定,因此我使用本文中的解决方案来解决该问题。 http://dotnet-redzone.blogspot.de/2012/11/windows-phone-8longlistselector.html
这是我的相关代码: 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>
我的 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); }
}
}
查看模型
/// <summary>
/// The currently selected Items.
/// </summary>
public ObservableCollection<File> SelectedFiles
{
get { return _selectedFiles; }
set { Set(() => this.SelectedFiles, ref _selectedFiles, value); }
}
private ObservableCollection<File> _selectedFiles;
但是这个解决方案不起作用。SelectedFiles 属性根本没有改变。(_selectedFiles 始终为空)
- 编辑: Set(() => this.SelectedFiles, ref _selectedFiles, value); 来自 Mvvmlight (Laurent Bugnion) 包。