使用Visual Studio 2013和Window Phone 8 SDK我无法获得SelectedItem
的{{1}}属性来正确绑定到MVVM属性。
对于在控件中包含但存在于SDK中但存在标记为已修复的错误,该问题似乎是一个相同的问题。 http://silverlight.codeplex.com/workitem/9360
是否有其他人遇到此问题且知道修复/更新版本?
我目前正在使用解决方法背后的代码
LongListSelector
答案 0 :(得分:2)
要获取选择的项目到ViewModel,我总是使用LongListSelector扩展 - 代码可以在这里找到:https://gist.github.com/Depechie/7524630
您需要做的是将其添加到LongListSelector的XAML中:
<phone:LongListSelector x:Name="List" ext:LongListSelectorExtension.Command="{Binding *YOURVIEWMODELCOMMAND*}" />
viewmodel上的命令将在LongListSelector上接收项源的对象类型
答案 1 :(得分:0)
使用行为
public class LongListSelectedItemBehavior : Behavior<LongListSelector>
{
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedItem. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(LongListSelectedItemBehavior), new PropertyMetadata(null));
protected override void OnAttached()
{
base.OnAttached();
if (AssociatedObject != null)
{
AssociatedObject.SelectionChanged += AssociatedObject_SelectionChanged;
}
}
protected override void OnDetaching()
{
base.OnDetaching();
if (AssociatedObject != null)
{
AssociatedObject.SelectionChanged -= AssociatedObject_SelectionChanged;
}
}
private void AssociatedObject_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
SelectedItem = AssociatedObject.SelectedItem;
}
和XAML
<phone:LongListSelector Margin="0,0,-22,0"
ItemsSource="{Binding Items}">
<i:Interaction.Behaviors>
<local:LongListSelectedItemBehavior SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
</i:Interaction.Behaviors>
</phone:LongListSelector>