WP8 LongListSelector SelectedItem not bindable

后端 未结 2 1799
有刺的猬
有刺的猬 2021-02-09 13:10

In WP8, they forgot to provide SelectedItem as a dependency property, hence I\'m not able to bind to it. I fixed that using this: http://dotnet-redzone.blogspot.com/2012/11/wind

相关标签:
2条回答
  • 2021-02-09 13:28

    It appears that the custom LongListSelector class that you are using does not handle the setter properly.

    Replace the OnSelectedItemChanged callback with the following:

        private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var selector = (LongListSelector)d;
            selector.SetSelectedItem(e);
        }
    
        private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
        {
            base.SelectedItem = e.NewValue;
        }
    
    0 讨论(0)
  • 2021-02-09 13:52

    And there is full version of these two parts:

    public class LongListSelector : Microsoft.Phone.Controls.LongListSelector
        {
            public LongListSelector()
            {
                SelectionChanged += LongListSelector_SelectionChanged;
            }
    
        void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedItem = base.SelectedItem;
        }
    
        public static readonly DependencyProperty SelectedItemProperty =
            DependencyProperty.Register(
                "SelectedItem",
                typeof(object),
                typeof(LongListSelector),
                new PropertyMetadata(null, OnSelectedItemChanged)
            );
    
        private static void OnSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var selector = (LongListSelector)d;
            selector.SetSelectedItem(e);
        }
    
        private void SetSelectedItem(DependencyPropertyChangedEventArgs e)
        {
            base.SelectedItem = e.NewValue;
        }
    
        public new object SelectedItem
        {
            get { return GetValue(SelectedItemProperty); }
            set { SetValue(SelectedItemProperty, value); }
        }
    }
    
    0 讨论(0)
提交回复
热议问题