Trouble binding LongListSelector.SelectedItem to MVVM property

前端 未结 2 918
遥遥无期
遥遥无期 2021-01-28 23:57

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

2条回答
  •  抹茶落季
    2021-01-29 00:41

    Use bahavior

    public class LongListSelectedItemBehavior : Behavior
    {
        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

        
                    
                        
                    
                
    

提交回复
热议问题