Trouble binding LongListSelector.SelectedItem to MVVM property

可紊 提交于 2019-12-20 06:07:42

问题


Using Visual Studio 2013 and the Window Phone 8 SDK I cannot get the SelectedItem property of the LongListSelector to properly bind to an MVVM property.

It appears to be an identical issue to a bug that existed in the control prior to its inclusion in the SDK but which is marked as fixed. http://silverlight.codeplex.com/workitem/9360

Is anyone else experiencing this and know of a fix/updated version?

I am currently using a code behind workaround

    private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LongListSelector s = sender as LongListSelector;
        var vm = DataContext as ViewModel.MainViewModel;
        Debug.Assert(vm != null);

        vm.SelectedLegislator = s.SelectedItem;   
    }

回答1:


To get the item that was selected to the ViewModel, I'm always using a LongListSelector Extension - the code can be found here: https://gist.github.com/Depechie/7524630

What you need to do is add it to the XAML of your LongListSelector:

<phone:LongListSelector x:Name="List" ext:LongListSelectorExtension.Command="{Binding *YOURVIEWMODELCOMMAND*}" />

The command on the viewmodel will receive the object type of your item source on the LongListSelector




回答2:


Use bahavior

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;
    }

And 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>


来源:https://stackoverflow.com/questions/20031964/trouble-binding-longlistselector-selecteditem-to-mvvm-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!