Silverlight 无法将ComboBoxItem.IsEnabled设置为绑定
我这一天充满了Silverlight的独特说法,包括这个小傻瓜:Silverlight 无法将ComboBoxItem.IsEnabled设置为绑定,silverlight,xaml,silverlight-4.0,Silverlight,Xaml,Silverlight 4.0,我这一天充满了Silverlight的独特说法,包括这个小傻瓜: <ComboBox> <ComboBox.ItemContainerStyle> <Style TargetType="ComboBoxItem"> <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/> </Style> </Com
<ComboBox>
<ComboBox.ItemContainerStyle>
<Style TargetType="ComboBoxItem">
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
</Style>
</ComboBox.ItemContainerStyle>
<ComboBoxItem>First</ComboBoxItem>
<ComboBoxItem>Second</ComboBoxItem>
</ComboBox>
如果我将{Binding IsEnabled}
更改为简单的True
或False
,那么它可以正常工作
我非常困惑,因为ComboBoxItem.IsEnabled
是一个依赖属性
,并且不是只读的,所以错误消息完全是垃圾
有没有办法解决这个问题?最后,我要做的就是将ComboxItem
s上的IsEnabled
属性绑定到视图模型上的属性
是的,我还尝试将
ItemsSource
绑定到我的视图模型集合,并确保IsEnabled
属性实际存在于我的视图模型上。同样的问题。我通过覆盖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中仍然不可能吗?对我来说,这似乎是完全荒谬的,就像我今天遇到的所有其他问题一样。也许这已经太离谱了(而且太晚了),但这可能与绑定对象上与您正在设置的属性匹配的属性的命名有关吗
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
如果路径不是
IsEnabled
,你能在XAML中执行吗?我知道这个线程很旧,但今天我遇到了同样的问题,我只想确认Dave Lowther所建议的就是这个案例的问题。
将属性名称从IsEnabled
更改为IsComboxItemEnabled
后,一切都开始正常工作,所以不要在模型中使用IsEnabled
名称
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>