示例#1
0
        public void WhenBehaviorIsAttached_ThenItSynchronizesTheInitialSelection()
        {
            var behavior = new SynchronizeSelectedItems();

            BindingOperations.SetBinding(
                behavior,
                SynchronizeSelectedItems.SelectionsProperty,
                new Binding("ObservableSelections"));

            var listBox = new ListBox()
            {
                SelectionMode = SelectionMode.Multiple
            };

            BindingOperations.SetBinding(
                listBox, ListBox.ItemsSourceProperty,
                new Binding("Items"));

            var itemsHolder =
                new ItemsHolder
            {
                Items = { "a", "b", "c", "d" },
                ObservableSelections = { "a", "c" }
            };

            listBox.DataContext = itemsHolder;

            var behaviors = Interaction.GetBehaviors(listBox);

            behaviors.Add(behavior);

            CollectionAssert.AreEquivalent(new[] { "a", "c" }, listBox.SelectedItems);
        }
        public void WhenDataContextChanges_ThenItupdatesSelectedItems()
        {
            var behavior = new SynchronizeSelectedItems();
            BindingOperations.SetBinding(
                behavior,
                SynchronizeSelectedItems.SelectionsProperty,
                new Binding("ObservableSelections"));

            var listBox = new ListBox() { SelectionMode = SelectionMode.Multiple };
            BindingOperations.SetBinding(
                listBox, ListBox.ItemsSourceProperty,
                new Binding("Items"));

            listBox.DataContext =
                new ItemsHolder
                {
                    Items = { "a", "b", "c", "d" },
                    ObservableSelections = { "a", "c" }
                };

            var behaviors = Interaction.GetBehaviors(listBox);
            behaviors.Add(behavior);

            listBox.DataContext =
                new ItemsHolder
                {
                    Items = { "a", "b", "c", "d" },
                    ObservableSelections = { "b", "d" }
                };

            CollectionAssert.AreEquivalent(new[] { "b", "d" }, listBox.SelectedItems);
        }
示例#3
0
        public void WhenSelectionsAreUpdatedOnTheObservableModelAfterDetachingTheBehavior_ThenSelectedItemsAreNotUpdatedOnTheListBox()
        {
            var behavior = new SynchronizeSelectedItems();

            BindingOperations.SetBinding(
                behavior,
                SynchronizeSelectedItems.SelectionsProperty,
                new Binding("ObservableSelections"));

            var listBox = new ListBox()
            {
                SelectionMode = SelectionMode.Multiple
            };

            BindingOperations.SetBinding(
                listBox, ListBox.ItemsSourceProperty,
                new Binding("Items"));

            var itemsHolder =
                new ItemsHolder
            {
                Items = { "a", "b", "c", "d" },
                ObservableSelections = { "a", "c" }
            };

            listBox.DataContext = itemsHolder;

            var behaviors = Interaction.GetBehaviors(listBox);

            behaviors.Add(behavior);

            itemsHolder.ObservableSelections.Add("d");

            behaviors.Remove(behavior);

            itemsHolder.ObservableSelections.Remove("c");

            CollectionAssert.AreEquivalent(new[] { "a", "c", "d" }, listBox.SelectedItems);
        }
        public void WhenSelectedItemsAreUpdatedOnTheListBox_ThenSelectionsAreUpdatedOnTheModel()
        {
            var behavior = new SynchronizeSelectedItems();
            BindingOperations.SetBinding(
                behavior,
                SynchronizeSelectedItems.SelectionsProperty,
                new Binding("ObservableSelections"));

            var listBox = new ListBox() { SelectionMode = SelectionMode.Multiple };
            BindingOperations.SetBinding(
                listBox, ListBox.ItemsSourceProperty,
                new Binding("Items"));

            var itemsHolder =
                new ItemsHolder
                {
                    Items = { "a", "b", "c", "d" },
                    ObservableSelections = { "a", "c" }
                };

            listBox.DataContext = itemsHolder;

            var behaviors = Interaction.GetBehaviors(listBox);
            behaviors.Add(behavior);

            listBox.SelectedItems.Add("d");
            listBox.SelectedItems.Remove("c");

            CollectionAssert.AreEquivalent(new[] { "a", "d" }, itemsHolder.ObservableSelections);
        }