System.Exception {System.ArgumentException} Value

2019-09-16 18:43发布

问题:

I'm getting this exception whenever i try to access m list picker control in my app.

+       this    {App_name.App}  App_name.App
+       sender  {App_name.App}  object {App_name.App}
-       e   {System.Windows.ApplicationUnhandledExceptionEventArgs} System.Windows.ApplicationUnhandledExceptionEventArgs
+       base    {System.Windows.ApplicationUnhandledExceptionEventArgs} System.EventArgs {System.Windows.ApplicationUnhandledExceptionEventArgs}
+       ExceptionObject {System.ArgumentException: Value does not fall within the expected range.}  System.Exception {System.ArgumentException}
        Handled false   bool
+       Non-Public members      

Code for my list picker is

<ListBox Margin="0,417,0,0">
            <ListBoxItem>
                <toolkit:ListPicker Name="LearnerFileChooser" Width="431" >
                    <toolkit:ListPickerItem Content="A" />
                    <toolkit:ListPickerItem Content="B" />
                    <toolkit:ListPickerItem Content="C" />
                    <toolkit:ListPickerItem Content="E" />
                    <toolkit:ListPickerItem Content="F" />
                    <toolkit:ListPickerItem Content="G" />
                    <toolkit:ListPickerItem Content="H" />
                </toolkit:ListPicker>
            </ListBoxItem>

If i reduce the no. of items to 4 then it works properly but it crashes on more then 4 items.

I'm trying to create a list of alphabets from which user can choose.

回答1:

It's a known issue.

You must bind the items to be able to use more than 5.

Explanation on Codeplex:

ListPicker as an ItemsControl, gets its Items property set to a list of ListPickerItems in your example. ListPickerItems are UIElements, and the ListPicker renders them in its presenter. When there are 5 or less items, the expanded mode opens on the current page, and you can see all the items in the presenter.

But when 6 or more items are present, opening the list picker goes to full mode which opens a new page. This new page has a listbox, which gets its items property set to the listpicker's items. This is where it breaks. By setting the listbox's items to the listpicker's items (in this case a list of listpickeritems), the listbox will put those UIElements into its view. Now a single listboxitem is included in two places on the visual tree.

Because of this issue, ListPicker only supports databinding and templating. DO NOT set the ListPicker's items to specific UIElements.