SelectedItemsSynchronizer.SH 1.0.0

dotnet add package SelectedItemsSynchronizer.SH --version 1.0.0                
NuGet\Install-Package SelectedItemsSynchronizer.SH -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SelectedItemsSynchronizer.SH" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SelectedItemsSynchronizer.SH --version 1.0.0                
#r "nuget: SelectedItemsSynchronizer.SH, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SelectedItemsSynchronizer.SH as a Cake Addin
#addin nuget:?package=SelectedItemsSynchronizer.SH&version=1.0.0

// Install SelectedItemsSynchronizer.SH as a Cake Tool
#tool nuget:?package=SelectedItemsSynchronizer.SH&version=1.0.0                

SelectedItemsBindingDemo

An example of how to bind the SelectedItems property of an ItemsControl in WPF to a ViewModel

The origin of all this and a good explanation of how it all works can be found at http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html

The original example demo is still included in the code base, but the code has been reorganised a bit so that it can be published as a utility library in a NuGet package. I'm hoping you'll find it as useful as I have done, and making it available as a NuGet package should ease integration with your production code!

Example usage

List<string> names;
ObservableCollection<string> selectedNames;
ListView listView;

[TestInitialize]
public void TestInitialize()
{
  names = new List<string>() { "Abraham", "Lincoln", "James", "Buchanan" };
  selectedNames = new ObservableCollection<string>();
  listView = new ListView();
  listView.ItemsSource = names;
  listView.SelectionMode = SelectionMode.Extended;
  MultiSelectorBehaviours.SetSynchronizedSelectedItems(listView, (IList)selectedNames);
}

[TestMethod]
public void InitialiseToNoSelection()
{
  this.selectedNames.Count().Should().Be(0);
}

[TestMethod]
public void ShouldSynchroniseListViewSelectAll()
{
  // Act
  this.listView.SelectAll();

  // Assert
  this.selectedNames.Count().Should().Be(this.names.Count());
}

[TestMethod]
public void ShouldSynchroniseListViewSetSelectedIndex()
{
  // Act
  this.listView.SelectedIndex = 0;

  // Assert
  this.selectedNames.Count().Should().Be(1);
}

[TestMethod]
public void ShouldSynchroniseListViewSetSelectedItem()
{
  //Arrange
  Random random = new Random(DateTime.Now.Millisecond);
  object itemToSelect = this.listView.Items.GetItemAt(random.Next(this.names.Count));

  // Act
  this.listView.SelectedItem = itemToSelect;

  // Assert
  this.selectedNames.Count().Should().Be(1);
}

[TestMethod]
public void ShouldSynchroniseListViewAddSingleSelectedItem()
{
  //Arrange
  Random random = new Random(DateTime.Now.Millisecond);
  object itemToSelect = this.listView.Items.GetItemAt(random.Next(this.names.Count));

  // Act
  this.listView.SelectedItems.Add(itemToSelect);

  // Assert
  this.selectedNames.Count().Should().Be(1);
}

[TestMethod]
public void ShouldSynchroniseListViewAddMultipleSelectedItems()
{
  // Act
  this.listView.SelectedItems.Add(this.listView.Items.GetItemAt(0));
  this.listView.SelectedItems.Add(this.listView.Items.GetItemAt(1));

  // Assert
  this.selectedNames.ShouldBeEquivalentTo(this.listView.SelectedItems, opt => opt.WithStrictOrdering());
}
Product Compatible and additional computed target framework versions.
.NET net6.0-windows7.0 is compatible.  net7.0-windows was computed.  net7.0-windows7.0 is compatible.  net8.0-windows was computed.  net8.0-windows7.0 is compatible. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.5

    • No dependencies.
  • .NETFramework 4.7.2

    • No dependencies.
  • net6.0-windows7.0

    • No dependencies.
  • net7.0-windows7.0

    • No dependencies.
  • net8.0-windows7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 126 4/2/2024