代码之家  ›  专栏  ›  技术社区  ›  Kent Boogaart

无法将ComboBoxitem.IsEnabled设置为绑定

  •  0
  • Kent Boogaart  · 技术社区  · 15 年前

    我有一天都有Silverlight的癖好,包括这个小笨蛋:

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

    上述失败原因如下:

      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: 
    

    如果我改变 {Binding IsEnabled} 简单地 True False ,然后就可以了。

    我很困惑,因为 ComboBoxItem.IsEnabled 是一个 DependencyProperty 而且是 只读,因此错误消息完全是垃圾。

    有什么解决办法吗?最后,我要做的就是 IsEnabled 属性上 ComboBoxItem 将绑定到视图模型上的属性。

    是的,我也试过装订 ItemsSource 到我的视图模型集合并确保 使能 属性实际上存在于我的视图模型中。同样的问题。

    3 回复  |  直到 13 年前
        1
  •  1
  •   Kent Boogaart    15 年前

    我在这方面做了很多工作 PrepareContainerForItemOverride 如下:

    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"));
    }
    

    这在SL4中真的不可能吗?在我看来完全荒谬,就像我今天遇到的所有其他问题一样。

        2
  •  0
  •   Dave Lowther    15 年前

    也许这离基准太远了(而且太晚了),但它是否与绑定对象上与您正在设置的属性匹配的属性的命名有关?

    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
    

    如果路径不是XAML,可以在XAML中执行此操作吗 IsEnabled ?

        3
  •  0
  •   Pete Irfan TahirKheli    13 年前

    我知道这条线索已经过时了,但今天我面临着同样的问题,我只想确认戴夫·洛瑟所建议的就是这个案例的问题。 在更改属性名之后 IsEnabled IsComboBoxItemEnabled 一切都开始正常工作,所以不要使用 使能 模型中的名称

    推荐文章