条件绑定(二维列表的属性)

时间:2014-08-01 10:08:36

标签: c# wpf mvvm binding

我有二维页面列表(ViewModels)来创建按钮的屏幕矩阵

public ObservableCollection<ObservableCollection<ViewModelPageBase>> Pages { get; private set; }

每个页面都有IsError依赖项属性,在其基类ViewModelPageBase中实现。如果设置了IsError,则相应按钮的文字将变为Red。一切正常,直到这里。

现在我想要关注:

  • 如果设置了任何ViewModels IsError,则将面板,托管按钮,背景设置为Yellow

我可以在每次更改内容时扫描所有页面(INotifyCollectionChanged. CollectionChanged),以检查其中一个是否有IsError = true并且具有要设置的单独依赖项属性,然后绑定到此属性以更改面板背景

但我在想,也许有办法直接绑定到IsError?可能吗?如果没有,什么是实现所需的正确解决方案?


以下是将List<List<T>>显示为按钮矩阵的方式

<!-- how to change color of ItemControl to Yellow, when item IsError is set? -->
<!-- rows -->
<ItemControl ItemsSource="{TemplateBinding Items}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <ItemContainerTemplate>
            <!-- columns -->
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <ItemContainerTemplate>
                        <!-- item -->
                        <!-- binding IsError to change button color works ok -->
                        <Button ... />
                    </ItemContainerTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </ItemContainerTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

0 个答案:

没有答案
相关问题