views:

62

answers:

1

I've had a day full of Silverlight idiosynchrasies, including this little doozy:

<ComboBox>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
        </Style>
    </ComboBox.ItemContainerStyle>

    <ComboBoxItem>First</ComboBoxItem>
    <ComboBoxItem>Second</ComboBoxItem>
</ComboBox>

The above fails with:

  System.Windows.Markup.XamlParseException occurred
    Message=Set property '' threw an exception. [Line: 88 Position: 52]
    LineNumber=88
    LinePosition=52
    StackTrace:
         at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
    InnerException: System.NotSupportedException
         Message=Cannot set read-only property ''.
         StackTrace:
              at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
              at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)
         InnerException: 

If I change {Binding IsEnabled} to simply True or False, then it works fine.

I'm utterly confused because ComboBoxItem.IsEnabled is a DependencyProperty and is not readonly, so the error message is complete rubbish.

Any ideas how to fix this? Ultimately, all I want to do is have the IsEnabled property on the ComboBoxItems to be bound to a property on my view model.

PS. Yes, I also tried binding ItemsSource to my view model collection and ensuring that the IsEnabled property actually existed on my view models. Same problem.

A: 

I worked around this for now by overriding PrepareContainerForItemOverride as follows:

protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
    base.PrepareContainerForItemOverride(element, item);

    // can't do this in ItemContainerStyle because SL is poo
    (element as ComboBoxItem).SetBinding(ComboBoxItem.IsEnabledProperty, new Binding("IsEnabled"));
}

Is this really still not possible in SL4? Seems completely ridiculous to me, as have all the other problems I've run into today.

Kent Boogaart