【问题标题】:Binding ListBoxItem's IsEnabled property with ItemTemplate set将 ListBoxItem 的 IsEnabled 属性与 ItemTemplate 集绑定
【发布时间】:2011-08-16 23:27:24
【问题描述】:

我有以下列表框:

<ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled"
         HorizontalAlignment="Stretch"
         HorizontalContentAlignment="Stretch"
         SelectionChanged="ListBoxContainerSelectionChanged"
         ItemsSource="{Binding Movies}"
         ItemContainerStyle="{StaticResource HeaderListBoxItemStyle}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Controls:MoviesItemControl Header="{Binding Title}"
                                        Detail="{Binding FormattedDescription}"
                                        Rating="{Binding Rating}"
                                        Opacity="{Binding IsSuppressed, Converter={StaticResource DimIfTrueConverter}}" 
                                        IsEnabled="{Binding IsSuppressed, Converter={StaticResource InverseBooleanConverter}}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我正在尝试将ListBoxItems 的禁用状态设置为“已抑制”(未找到描述的电影)。我有一个可以绑定到我的个人控件的属性,但我希望它们在实际列表中不可选择。 (并使用我的ItemsContainerStyle 中包含的禁用状态)

我在 SO 上看到了一些使用 Trigger 的实现,但这似乎在 WP7 中不可用,我宁愿不必为每个控件创建不同的样式以便它们正确绑定。

有什么想法吗?

【问题讨论】:

    标签: wpf xaml data-binding windows-phone-7


    【解决方案1】:

    查看以下问题:WP7 - Databind ListboxItem's IsEnabled Property

    这又链接到这个:Better SetterValueBindingHelper makes Silverlight Setters better-er!

    我通过David Anson 尝试了SetterValueBindingHelper 针对这个特定场景,效果很好。您所要做的就是将 SetterValueBindingHelper.cs 添加到您的项目中,然后您可以像这样在 setter 中绑定IsEnabled

    <Style x:Key="HeaderListBoxItemStyle" TargetType="ListBoxItem">
        <Setter Property="delay:SetterValueBindingHelper.PropertyBinding">
            <Setter.Value>
                <delay:SetterValueBindingHelper Property="IsEnabled"
                                                Binding="{Binding IsSuppressed}"/>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

    • 谢谢!我专门为这个列表创建了一个新的Style,因为不是我所有的models 都有这个IsSuppressed标志,但我想要相同的交互。有没有办法只使用 1 个Style 来实现这一点,而无需将该属性添加到每个model
    猜你喜欢
    • 1970-01-01
    • 2021-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多