我在另一个ItemsControl的DataTemplate中嵌套了一个ItemsControl。这似乎是从二维数组中显示数字网格的最简单方法,它做得非常好。我的问题是,我想改变颜色,一个特定的数字在网格中。我希望触发两个AlternationIndex的ItemsControls,这样我就可以准确地识别要突出显示的数字。
在父DataContext的ItemsControl中,我有一个二维整数数组,如下所示:
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或其他什么工具来完成它,但是现在我对如何实现这个binding...if非常感兴趣--它甚至是可能的。
更新:
@d.moncada给了我一个提示,我需要找出我做错了什么。我不需要寻找ItemsControl类型的祖先,而是需要查找ContentPresenter。
发布于 2014-08-01 17:34:59
这就是你要的。我能够通过寻找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>https://stackoverflow.com/questions/25084231
复制相似问题