繁体   English   中英

为什么此附加属性未更新?

[英]Why is this attached property not updating?

好吧,所以我本来只是将其发布为为什么绑定无法正常工作的问题。 但是,当我尝试最小化要在SO上发布的代码时,这又产生了另一个问题。

因此,从本质上讲, Extensions类旨在挂接到ListBox并制作SelectedItems的可绑定版本。 抓住SelectedItems并将其放置在Selected Attached属性中的这种功能在我的程序中有效(在我的真实程序中,它似乎不绑定吗?),但在此最小化版本中不起作用。 我不知道为什么代码似乎可以完成所有需要做的事情。

我用来测试的代码:

.xaml.cs

namespace MyNamespace
{
    public partial class MainWindow
    {
        public IList Selected { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            Selected = new List<object>();
            Why.ItemsSource = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        }
        private void Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(Selected.Cast<object>().Aggregate("", (s, info) => $"{s}{info}, ").TrimEnd(',', ' '));
            MessageBox.Show(Extensions.GetSelected(Why).Cast<object>().Aggregate("", (s, o) => $"{s}{o}, ").TrimEnd(',', ' '));
        }
    }
    public static class Extensions
    {
        public static readonly DependencyProperty SelectedProperty = DependencyProperty.RegisterAttached(
            "Selected", typeof(IList), typeof(Extensions), new PropertyMetadata(default(IList), HookSelectionChanged));

        private static void HookSelectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ListBox lb = sender as ListBox;
            if (lb == null)
                throw new ArgumentException("This property currently only supports DependencyObjects inheriting from Selector.", nameof(sender));
            lb.SelectionChanged += SelectionChanged;
        }

        private static void SelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
            => SetSelected((ListBox)sender, ((ListBox)sender).SelectedItems.Cast<object>().ToList());

        public static void SetSelected(DependencyObject element, IList value) => element.SetValue(SelectedProperty, value);

        public static IList GetSelected(DependencyObject element) => (IList)element.GetValue(SelectedProperty);
    }
}

.xaml

<Window x:Class="MyNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:lcl="clr-namespace:MyNamespace"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="My title." Height="350" Width="425" MaxHeight="350" MaxWidth="425" MinHeight="350" MinWidth="425">
    <StackPanel>
        <ListBox lcl:Extensions.Selected="{Binding Selected}" x:Name="Why" SelectionMode="Extended"/>
        <Button Click="Click" Content="blah"/>
    </StackPanel>
</Window>

任何想法都很棒! 谢谢 :)

好吧,这就是我错过了两件事:

  • 我没有在测试中实现INotifyPropertyChanged
  • 我不知道Binding并非始终都默认为TwoWay

因此,所有需要做的就是:

.xaml.cs

namespace MyNamespace
{
    public partial class MainWindow : INotifyPropertyChanged
    {
        private IList _selected;

        public IList Selected
        {
            get { return _selected; }
            set
            {
                if (Equals(value, _selected)) return;
                _selected = value;
                OnPropertyChanged();
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            Selected = new List<object>();
            Why.ItemsSource = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        }
        private void Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(Selected.Cast<object>().Aggregate("", (s, info) => $"{s}{info}, ").TrimEnd(',', ' '));
            MessageBox.Show(Extensions.GetSelected(Why).Cast<object>().Aggregate("", (s, o) => $"{s}{o}, ").TrimEnd(',', ' '));
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public class Extensions
    {
        public static readonly DependencyProperty SelectedProperty = DependencyProperty.RegisterAttached(
            "Selected", typeof(IList), typeof(Extensions), new FrameworkPropertyMetadata(default(IList), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, HookSelectionChanged));

        private static void HookSelectionChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ListBox lb = sender as ListBox;
            if (lb == null)
                throw new ArgumentException("This property currently only supports DependencyObjects inheriting from Selector.", nameof(sender));
            lb.SelectionChanged += SelectionChanged;
        }

        private static void SelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
            => SetSelected((ListBox)sender, ((ListBox)sender).SelectedItems.Cast<object>().ToList());

        public static void SetSelected(DependencyObject element, IList value) => element.SetValue(SelectedProperty, value);

        public static IList GetSelected(DependencyObject element) => (IList)element.GetValue(SelectedProperty);
    }
}

而且效果很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM