4

A UWP ComboBox is generating a StackOverflowException.

<Grid Name="CutoutsGrid"
    <ListView 
        <ComboBox 
            ItemsSource="{x:Bind CutoutsList}"
            SelectedItem="{x:Bind CutShape,Mode=TwoWay}"
        />
    </ListView>
</Grid>

CutShape and CutoutsList are defined in the ViewModel

public class ViewModel : ViewModelBase
{
    string _CutShape = default(string);
    public string CutShape { get { return _CutShape; } set { Set(ref _CutShape, value); } }

    public List<Cutout> CutoutsList { get { return MatboardService.GetCutoutsList(); } }

CutoutsList returns 5 cutouts to choose from.

public class Cutout : BindableBase
{
    string _ItemCode = default(string);
    public string ItemCode { get { return _ItemCode; } set { Set(ref _ItemCode, value); } }

    string _ItemDescription = default(string);
    public string ItemDescription { get { return _ItemDescription; } set { Set(ref _ItemDescription, value); } }

    decimal _MinutesLabor = default(decimal);
    public decimal MinutesLabor { get { return _MinutesLabor; } set { Set(ref _MinutesLabor, value); } }
}

The StackOverflowException occurs in Page.g.cs

public static void Set_Windows_UI_Xaml_Controls_Primitives_Selector_SelectedItem(global::Windows.UI.Xaml.Controls.Primitives.Selector obj, global::System.Object value, string targetNullValue)
{
    if (value == null && targetNullValue != null)
    {
        value = (global::System.Object) global::Windows.UI.Xaml.Markup.XamlBindingHelper.ConvertValue(typeof(global::System.Object), targetNullValue);
    }
    obj.SelectedItem = value;
}

In this block value == "" and targetNullValue == null. The exception occurs on the line obj.SelectedItem = value;

Although the ComboBox is in a ListView in a Grid, the exception occurs on the first call to

public List<Cutout> CutoutsList { get { return MatboardService.GetCutoutsList(); } }

However that is followed by many (I counted 60 before I stopped counting) calls to

Set_Windows_UI_Xaml_Controls_Primitives_Selector_SelectedItem

Why is this ComboBox causing a StackOverflowException?

Vague
  • 2,198
  • 3
  • 18
  • 46

1 Answers1

1

You can quite likely make this work by either removing the TwoWay-binding or by using the older Binding-method.

For example like this::

<Grid Name="CutoutsGrid"
    <ListView 
        <ComboBox 
            ItemsSource="{Binding CutoutsList}"
            SelectedItem="{Binding CutShape,Mode=TwoWay}"
        />
    </ListView>
</Grid>

The actual problem seems to be that the StackOverflowException is caused because your setter in VM's CutShape is quite likely notifying about the changed value even though it doesn't change. So you can also try:

public string CutShape 
{ 
  get { return _CutShape; } 
  set 
  { 
    if (_CutShape != value)
       Set(ref _CutShape, value);  
  }
}

I'm not sure what the Set-method is doing but you can quite likely get rid of the ref-keyword.

Mikael Koskinen
  • 12,306
  • 5
  • 48
  • 63