XAML bind SelectedItem to a DataContext.listbox reference

86 Views Asked by At

I stuck in a Problem: I have a PopUp window, its DataContext points to an object which holds a reference to a ListBox (reftolistbox).

I managed to create a working binding with this codebehind code:

private void ID_Loaded(object sender, RoutedEventArgs e)
    {
        Binding myBinding = new Binding("id");
        myBinding.Source = ((myclass)DataContext).reftolistbox;
        myBinding.Path = new System.Windows.PropertyPath("SelectedItem.Name");
        BindingOperations.SetBinding(ID, ComboBox.TextProperty, myBinding);
    }

I want to replace the above code with a XAML Solution, here is a list i tried but no one worked.

<Combobox ...
     Text="{Binding Source=DataContext.reftolistbox, Path=SelectedItem.Name }"  
     Text="{Binding reftolistbox.SelectedItem.Name }"  
     Text="{Binding Path=DataContext.reftolistbox.SelectedItem.Name}"

Need a XAML Solution, what am I doing wrong?

1

There are 1 best solutions below

0
On

XAML only binds to properties

DataContext:
              { public ListBox reftolistbox { get; set; } }

working binding in XAML:
              Text="{Binding reftolistbox.SelectedItem.Name }"