views:

850

answers:

2

The following code works fine in WPF.

In Silverlight it gives me the error **Invalid attribute value {Binding ElementName=WhichNumber, Path=SelectedItem.Content} for property Text. **

How can I get this to work in Silverlight?

    <ComboBox x:Name="WhichNumber" Width="100" HorizontalAlignment="Left" Margin="10" SelectedIndex="0">
        <ComboBoxItem Content="One"/>
        <ComboBoxItem Content="Two"/>
        <ComboBoxItem Content="Three"/>
    </ComboBox>

    <TextBlock Text="{Binding ElementName=WhichNumber, Path=SelectedItem.Content}"/>
+2  A: 

Silverlight doesn't support Relative Binding (binding the attribute of one element to the value of another element's attribute value) while WPF has full support for that kind of binding.

Justin Niessner
+2  A: 

Or you could move to Silverlight 3 which introduces UI element to element binding :)

For some reason (why they did this is beyond me), the syntax is slightly different, instead of writing when using WPF:

 <TextBlock Text="{Binding ElementName=WhichNumber, Path=SelectedItem.Content}"/>

you would write with Silverlight 3:

 <TextBlock Text="{Binding ElementName=WhichNumber, SelectedItem.Content}"/>

so without the 'Path=' part.

Unfortunately the Silverlight people at Microsoft have a tendency to make small changes to syntax and other things, rather than striving for easy code reuse across WPF and Silverlight.

eriksmith200
You can actually use the 'Path=' part in Silverlight as well if you want.
mattmanser