【问题标题】:Binding to AlternationCount of ItemsControl from ItemTemplate of a nested ItemsControl从嵌套 ItemsControl 的 ItemTemplate 绑定到 ItemsControl 的 AlternationCount
【发布时间】:2014-09-24 21:26:26
【问题描述】:

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

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

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

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

这是我的 ItemsControls XAML:

    <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 或其他东西来做,但现在我真的对如何做这个绑定很感兴趣……如果可能的话。

更新:

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

【问题讨论】:

    标签: wpf templatebinding multidatatrigger


    【解决方案1】:

    给你。我可以通过寻找ContentPresenter 而不是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,而我应该寻找一个 ContentPresenter。该死的。 :|感谢您的帮助。
    • 你给了我我想要的东西,但你的解决方案使列和行都变成红色。我只想让行和列的交集(只有一个数字)变成红色。但是,由于您引导我找到了我正在寻找的答案,如果您将答案更改为 MultiDataTrigger,就像我所做的那样,我会将您的答案标记为正确。 :) 再次感谢!
    • 已更新。谢谢。我实际上是根据您为我之前的回答留给我的评论来考虑整个列和行。很高兴我能提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    • 2011-03-27
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-30
    相关资源
    最近更新 更多