【发布时间】: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