locked
Binding to SelectedValue when a combo box is inside another control

    Question

  • I have quite a bizarre problem with this control (which contains a combo box). This code works perfectly in WPF and Silverlight:

    <local:TestingComboBox x:Name="TestComboBox">
                <x:String>Yes</x:String>
                <x:String>No</x:String>
    </local:TestingComboBox>

    Basically when I try and bind the selected value to the combo box (TestProperty=="No"):

    BindingOperations.SetBinding(TestComboBox, TestingComboBox.SelectedValueProperty, new Binding { Path = new PropertyPath("TestProperty"), Source = this, Mode = BindingMode.TwoWay});

    It throws this exception:
    System.ArgumentException was unhandled by user code
      HResult=-2147024809
      Message=Value does not fall within the expected range.
      Source=Windows.UI.Xaml
      StackTrace:
           at Windows.UI.Xaml.Data.BindingOperations.SetBinding(DependencyObject target, DependencyProperty dp, BindingBase binding)
           at ItemBug.MainPage.MainPage_Loaded(Object sender, RoutedEventArgs e)

    Within TestingComboBox, I set selected value to bind onto the actual combo box:

    BindingOperations.SetBinding(ChildControl, Selector.SelectedValueProperty, new Binding
    {
        Path = new PropertyPath("SelectedValue"),
        Source = this,
        Mode = BindingMode.TwoWay
    });

    So the assumption would be that the selected value does not currently exist in the available items. But this is not the case. I have verified that the items includes the value I am trying to select.

    If I use a combo box directly in the code, rather than within another control it works.

    If I use an items source rather than directly adding the items in the Xaml, it also works.

    All three of these scenarios are demonstrated in the application I have put on onedrive:

    https://onedrive.live.com/redir?resid=E862238D79090552!602&authkey=!APt4y1bSNtgdA4g&ithint=file%2c.zip

    Sample includes a WPF application which proves that from a Xaml perspective it should work (I have also verified that this works correctly in Silverlight).

    So the question is how do I get selectedvalue correctly binding and not given me a bizarre exception?

    Thanks,
    Stefan

    Monday, May 19, 2014 12:23 AM

Answers

  • Hi Stefan - thanks for the repro app.  I'll file a bug on this.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, May 20, 2014 1:05 PM
    Moderator

All replies

  • Hi Stefan - thanks for the repro app.  I'll file a bug on this.


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, May 20, 2014 1:05 PM
    Moderator
  • Thanks Matt!
    Wednesday, May 21, 2014 8:13 PM