Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiSelectorHelper.SelectedItems throws Exception when an Item is selected from a ListBox which is nested inside a ListView #4461

Open
LiTo96 opened this issue Feb 6, 2024 · 0 comments
Labels

Comments

@LiTo96
Copy link

LiTo96 commented Feb 6, 2024

Describe the bug

I have a ListBox inside a ListView and the ListView is binding SelectedItems via MultiSelectorHelper.SelectedItems to the ViewModel.
When I select an item from the ListBox (i.e. I click on that item) I get the following Exception:

System.ArgumentException: The value "Dog" is not of type "MahAppsTest.Person" and cannot be used in this generic collection. (Parameter 'value')
   at System.Collections.ObjectModel.Collection`1.System.Collections.IList.Add(Object value)
   at MahApps.Metro.Controls.MultiSelectorHelper.MultiSelectorBinding.OnSelectionChanged(Object sender, SelectionChangedEventArgs e) in C:\projects\mahapps-metro\src\MahApps.Metro\Controls\Helper\MultiSelectorHelper.cs:line 178
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.Controls.ListBox.OnSelectionChanged(SelectionChangedEventArgs e)
   at System.Windows.Controls.Primitives.Selector.SetSelectedHelper(Object item, FrameworkElement UI, Boolean selected)
   at System.Windows.Controls.Primitives.Selector.NotifyIsSelectedChanged(FrameworkElement container, Boolean selected, RoutedEventArgs e)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.Controls.ListBoxItem.OnIsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
   at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
   at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
   at System.Windows.DependencyObject.SetCurrentValueInternal(DependencyProperty dp, Object value)
   at System.Windows.Controls.ListBox.NotifyListItemClicked(ListBoxItem item, MouseButton mouseButton)
   at System.Windows.Controls.ListBoxItem.OnMouseLeftButtonDown(MouseButtonEventArgs e)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunDispatcher(Object ignore)
   at System.Windows.Application.RunInternal(Window window)
   at MahAppsTest.App.Main()

The following Code can reproduce the exception:

MainWindowViewModel.cs

public class MainWindowViewModel
{
  public MainWindowViewModel()
  {
    AllPersons.Add(new Person("Bob", ["Cat", "Dog"]));
    AllPersons.Add(new Person("Anna", ["Dog", "Mouse"]));
  }

  public ObservableCollection<Person> AllPersons { get; } = [];
  public ObservableCollection<Person> SelectedPersons { get; } = [];
}

MainWindow.xaml

<ListView ItemsSource="{Binding AllPersons}" mah:MultiSelectorHelper.SelectedItems="{Binding SelectedPersons}">
	<ListView.ItemTemplate>
		<DataTemplate DataType="{x:Type mahAppsTest:Person}">
			<Grid>
				<Grid.RowDefinitions>
					<RowDefinition Height="Auto" />
					<RowDefinition Height="*" />
				</Grid.RowDefinitions>
				<TextBlock Grid.Row="0" HorizontalAlignment="Center" Text="{Binding FirstName}" />
				<ListBox Grid.Row="1" Background="Transparent" ItemsSource="{Binding Pets}" />
			</Grid>
		</DataTemplate>
	</ListView.ItemTemplate>
</ListView>

Person.cs

public record Person(string FirstName, string[] Pets);

Steps to reproduce

  1. Define a ListView and bind its SelectedItems to the ViewModel
  2. Define a ListBox inside the ListView
  3. Start App
  4. Click on Item inside the ListBox
  5. Boom!

Expected behavior

I'm not sure if the default WPF behavior is to bubble up the SelectionChanged Event or not.
Should clicking on an Item in the nested ListBox only select that Item or should it also select the Item in the enclosing ListView? I'm not sure tbh. But it would be nice if it wouldn't crash.

Actual behavior

Application crashes because of the Exception.

Environment

MahApps.Metro version: v3.0.0-alpha0476
Target Framework: .Net 8

Screenshot

image

@LiTo96 LiTo96 added the Bug label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant