Binding dependency object to ComboBox selected item value RRS feed

  • Question

  • Hi,
    using Visual C# 2010 Express,
    in UserControlA, I have a combobox cbOraDS that's populated with code behind as follow:

    private void Grid_Loaded(object sender, RoutedEventArgs e)
        DataTable dt = FunctionToInitialize();
        cbOraDS.DisplayMemberPath = dtDataSources.Columns[0].ColumnName;
        cbOraDS.SelectedValuePath = dtDataSources.Columns[0].ColumnName;
        cbOraDS.DataContext = dtDataSources;
        if (dtDataSources.Rows.Count > 0)
            cbOraDS.SelectedIndex = 0;

    At runtime, the combobox is loaded fine and I can select values from the drop down list

    Now, UserControlA also calls UserControlB and binds dependency object of UserControlB->OraDS with the selected item value of the UserControlA->cbOraDS

    Basically UserControlA should pass it's cbOraDS selected item value to UserControlB object OraDS

       OraDS="{Binding ElementName=cbOraDS, Path=SelectedValuePath, RelativeSource={RelativeSource AncestorType=UserControl},Mode=OneWay}">

    At compile time I get this error:
    System.Windows.Markup.XamlParseException occurred
      Message='Set property 'System.Windows.Data.Binding.RelativeSource' threw an exception.' Line number '56' and line position '40'.




    Wednesday, August 1, 2012 1:10 AM


  • got it, I should not have passed the Relative Source attribute in the binding statement


    • Marked as answer by ysrini Wednesday, August 1, 2012 1:31 AM
    Wednesday, August 1, 2012 1:31 AM