late绑定到ItemsControl的AlternateCou

late绑定到ItemsControl的AlternateCou

本文介绍了从嵌套ItemsControl的ItemTemplate绑定到ItemsControl的AlternateCount的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ItemsControl嵌套在另一个ItemsControl的DataTemplate中.这似乎是显示二维数组中的数字网格的最简单方法,效果非常好.我的问题是我想在网格中将颜色更改为特定数字.我希望触发两个ItemsControl的AlternateIndex,以便我可以准确地标识出要突出显示的数字.

I have an ItemsControl nested in the DataTemplate of another ItemsControl. This seemed like the easiest way to display a grid of numbers from a two-dimensional array, which it did very nicely. The problem I have is that I want to change the color a particular number in the grid. I was hoping to trigger off the AlternationIndex of both ItemsControls, so I can identify exactly which number to highlight.

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

In the DataContext for the parent ItemsControl I have a 2-D array of integers like this:

    public int[][] Grid
    {
        get { return _grid; }
    }

_grid初始化为20x20的数字数组.

_grid is initialized to a 20x20 array of numbers.

这是我的ItemsControls XAML:

Here is my XAML for the ItemsControls:

    <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或其他东西来做,但是现在我真的很感兴趣如何进行这种绑定……甚至有可能.

I can easily get an entire column of the numbers to be colored red if I leave the second condition off the MultiDataTrigger, but I can't get the second condition to work. Any thoughts on how I can do this? I can probably do it with a DataGrid or something, but now I'm really interested in how to do this binding...if it is even possible.

更新:

@ d.moncada给了我提示,我需要弄清楚自己做错了什么.我无需查找ItemsControl类型的祖先,而是需要查找ContentPresenter.

@d.moncada gave me the hint I needed to figure out what I had done wrong. Instead of looking for an ancestor of type ItemsControl, I needed to look for ContentPresenter.

推荐答案

在这里.我可以通过查找ContentPresenter而不是ItemsControl来实现这一点.

Here you go. I was able to achieve this by looking for the ContentPresenter rather than the ItemsControl.

    <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>

这篇关于从嵌套ItemsControl的ItemTemplate绑定到ItemsControl的AlternateCount的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 21:46