如果我在同一个 window 中使用带有两个控件的附加 属性,它不起作用

If I use an attached property with two controls in the same window, it doesn't work

我附上了这个 属性:

public class ListBoxSelectedItemsAttachedProperty
    {
        #region SelectedItems
        private static ListBox list;

        public static readonly DependencyProperty SelectedItemsProperty =
        DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
        typeof(ListBoxSelectedItemsAttachedProperty),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
            ,new PropertyChangedCallback(OnSelectedItemsChanged)
            ));

        public static IList GetSelectedItems(DependencyObject d)
        {
            return (IList)d.GetValue(SelectedItemsProperty);
        }

        public static void SetSelectedItems(DependencyObject d, IList value)
        {
            d.SetValue(SelectedItemsProperty, value);
        }

        private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ListBox listBox = (ListBox)d;
            list = listBox;
            listBox.SelectionChanged += listBox_SelectionChanged;
            listBox.Unloaded += listBox_Unloaded;
        }

        private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            IEnumerable listBoxSelectedItems = list.SelectedItems;
            IList ModelSelectedItems = GetSelectedItems(list);

            ModelSelectedItems.Clear();

            if (list.SelectedItems != null)
            {
                foreach (var item in list.SelectedItems)
                    ModelSelectedItems.Add(item);
            }
            SetSelectedItems(list, ModelSelectedItems);
        }




        private static void listBox_Unloaded(object sender, RoutedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            listBox.SelectionChanged -= listBox_SelectionChanged;
            listBox.Unloaded -= listBox_Unloaded;
        }
        #endregion
    }

我的 window 中有两个列表框,它们使用这个附加的 属性,这样:

<ListBox HorizontalAlignment="Left" Margin="5,0,5,0" Name="lsbGenerosAsignados" VerticalAlignment="Stretch" Width="Auto" MinWidth="130" Grid.Column="2" Grid.Row="1"
                 SelectionMode="Extended"
                 ItemsSource="{Binding Collection1InViewModel}"
                 DisplayMemberPath="Name"
                 Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding Property1InViewModel}"> 


    <ListBox HorizontalAlignment="Left" Margin="5,0,5,0" Name="lsbGenerosAsignados" VerticalAlignment="Stretch" Width="Auto" MinWidth="130" Grid.Column="2" Grid.Row="1"
                 SelectionMode="Extended"
                 ItemsSource="{Binding Collection2InViewModel}"
                 DisplayMemberPath="Name"
                 Behaviors:ListBoxSelectedItemsAttachedProperty.SelectedItems="{Binding Property2InViewModel}">

但是,当第一个 listBox 中的选择发生变化时,不会通知视图模型。如果我从第二个列表框中删除 de attached 属性,那么它会按预期工作。

如何将这个附加的 属性 与两个 listoBox 一起使用并通知视图模型属性?

谢谢。

这不起作用,因为您设置了全局

private static ListBox list;

当您有 2 个列表时,第二个列表将覆盖您的字段 'list',因此它不起作用。试试这个:

public class ListBoxSelectedItemsAttachedProperty
{
    #region SelectedItems

    public static readonly DependencyProperty SelectedItemsProperty; 

    static ListBoxSelectedItemsAttachedProperty()
    {
        FrameworkPropertyMetadata MetaData = new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault
                                             , new PropertyChangedCallback(OnSelectedItemsChanged));

        SelectedItemsProperty =  DependencyProperty.RegisterAttached("SelectedItems", typeof(IList),
                                 typeof(ListBoxSelectedItemsAttachedProperty),
                                 MetaData);
    }

    public static IList GetSelectedItems(DependencyObject d)
    {
        return (IList)d.GetValue(SelectedItemsProperty);
    }

    public static void SetSelectedItems(DependencyObject d, IList value)
    {
        d.SetValue(SelectedItemsProperty, value);
    }


    private static void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ListBox listBox = (ListBox)d;
        listBox.SelectionChanged +=  listBox_SelectionChanged;
        listBox.Unloaded += listBox_Unloaded;
    }

    private static void listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var list = (ListBox)sender;
        IEnumerable listBoxSelectedItems = list.SelectedItems;
        IList ModelSelectedItems = GetSelectedItems(list);

        ModelSelectedItems.Clear();

        if (list.SelectedItems != null)
        {
            foreach (var item in list.SelectedItems)
                ModelSelectedItems.Add(item);
        }
        SetSelectedItems(list, ModelSelectedItems);
    }




    private static void listBox_Unloaded(object sender, RoutedEventArgs e)
    {
        ListBox listBox = sender as ListBox;
        listBox.SelectionChanged -= listBox_SelectionChanged;
        listBox.Unloaded -= listBox_Unloaded;
    }
    #endregion
}