Search code examples
silverlightxamlbindingequivalent

What is the XAML equivalent for binding SelectedIndexProperty in .cs .net silverlight code?


I have been playing with bindings in silverlight, and have figured out how to bind in code, but would prefer to keep the binding in the XAML.

This is the code that works in my .cs file:

  System.Windows.Data.Binding IDBinding = new System.Windows.Data.Binding("ID");
            IDBinding.Source = MyTrans;
            IDBinding.Mode = System.Windows.Data.BindingMode.TwoWay;
            cbComboBox.SetBinding(ComboBox.SelectedIndexProperty, IDBinding);

this is my XAML line:

<ComboBox x:Name="cbComboBox"   Margin="4,20,6,0" Foreground="#FFD41D1D" Height="25" VerticalAlignment="Top">

How do I express the same thing in XAML?

Thanks!

-Ray


Solution

  • Assuming that you set the DataContext of the parent control of the ComboBox (or on the combobox itself) to MyTrans, the following should do it:

    <ComboBox x:Name="cbComboBox" SelectedIndex="{Binding ID, Mode=TwoWay}" />