首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从嵌套AlternationCount的ItemTemplate绑定到ItemsControl的ItemTemplate

从嵌套AlternationCount的ItemTemplate绑定到ItemsControl的ItemTemplate
EN

Stack Overflow用户
提问于 2014-08-01 16:01:22
回答 1查看 1.1K关注 0票数 0

我在另一个ItemsControl的DataTemplate中嵌套了一个ItemsControl。这似乎是从二维数组中显示数字网格的最简单方法,它做得非常好。我的问题是,我想改变颜色,一个特定的数字在网格中。我希望触发两个AlternationIndex的ItemsControls,这样我就可以准确地识别要突出显示的数字。

在父DataContext的ItemsControl中,我有一个二维整数数组,如下所示:

代码语言:javascript
复制
    public int[][] Grid
    {
        get { return _grid; }
    }

_grid被初始化为一个20x20的数字数组。

下面是ItemsControls的XAML:

代码语言:javascript
复制
    <ItemsControl Grid.Row="1"
                  Margin="5"
                  Name="RowItems"
                  ItemsSource="{Binding Path=Grid}"
                  AlternationCount="20"
                  HorizontalAlignment="Center">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl Name="ColumnItems"
                              ItemsSource="{Binding}"
                              AlternationCount="20">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"
                                        Margin="0"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Margin="4,2"
                                       Text="{Binding StringFormat={}{0:D2}}">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <MultiDataTrigger>
                                                <MultiDataTrigger.Conditions>
                                                    <Condition Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=TemplatedParent}}"
                                                               Value="8"/>
                                                    <Condition Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type ItemsControl}}}"
                                                               Value="6"/>
                                                </MultiDataTrigger.Conditions>
                                                <Setter Property="Foreground" Value="Red"/>
                                            </MultiDataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

如果我将第二个条件从MultiDataTrigger中去掉,我可以很容易地得到整列的数字被着色为红色,但是我不能让第二个条件工作。对我怎么做有什么想法吗?我可能可以用DataGrid或其他什么工具来完成它,但是现在我对如何实现这个binding...if非常感兴趣--它甚至是可能的。

更新:

@d.moncada给了我一个提示,我需要找出我做错了什么。我不需要寻找ItemsControl类型的祖先,而是需要查找ContentPresenter。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-01 17:34:59

这就是你要的。我能够通过寻找ContentPresenter而不是ItemsControl来实现这一点。

代码语言:javascript
复制
    <ItemsControl Grid.Row="1"
              Margin="5"
              Name="RowItems"
              ItemsSource="{Binding Path=Grid}"
              AlternationCount="20"
              HorizontalAlignment="Center">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <ItemsControl Name="ColumnItems"
                          ItemsSource="{Binding}"
                          AlternationCount="20">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal"
                                    Margin="0"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Margin="4,2"
                                   Text="{Binding StringFormat={}{0:D2}}">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">                      
                                        <Style.Triggers>
                                            <MultiDataTrigger>
                                                <MultiDataTrigger.Conditions>
                                                    <Condition Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}" Value="8"/>
                                                    <Condition Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorLevel=2, AncestorType={x:Type ContentPresenter}}}" Value="6"/>
                                                </MultiDataTrigger.Conditions>
                                                <Setter Property="Foreground" Value="Red"/>
                                            </MultiDataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25084231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档