Wpf 从嵌套ItemsControl的ItemTemplate绑定到ItemsControl的AlternationCount
我在另一个ItemsControl的DataTemplate中嵌套了一个ItemsControl。这似乎是从二维数组中显示数字网格的最简单方法,它做得非常好。我遇到的问题是,我想更改网格中特定数字的颜色。我希望触发两个ItemsControl的AlternationIndex,这样我就可以准确地识别要突出显示的数字 在父项控件的DataContext中,我有一个2-D整数数组,如下所示:Wpf 从嵌套ItemsControl的ItemTemplate绑定到ItemsControl的AlternationCount,wpf,templatebinding,multidatatrigger,Wpf,Templatebinding,Multidatatrigger,我在另一个ItemsControl的DataTemplate中嵌套了一个ItemsControl。这似乎是从二维数组中显示数字网格的最简单方法,它做得非常好。我遇到的问题是,我想更改网格中特定数字的颜色。我希望触发两个ItemsControl的AlternationIndex,这样我就可以准确地识别要突出显示的数字 在父项控件的DataContext中,我有一个2-D整数数组,如下所示: public int[][] Grid { get { return _gr
public int[][] Grid
{
get { return _grid; }
}
_网格初始化为20x20的数字数组
以下是ItemsControl的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、 蒙卡达给了我一个提示,我需要找出我做错了什么。我需要查找ContentPresenter,而不是寻找ItemsControl类型的祖先。给你。我可以通过查找
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>
Ha!所以,唯一的区别是我在寻找ItemsControl,而我本应该寻找ContentPresenter。该死谢谢你的帮助。你给了我我想要的,但是你的解决方案使列和行都变成红色。我只是想让行和列的交叉点(只有一个数字)变成红色。但是,既然你引导我找到了我一直在寻找的答案,如果你像我一样将答案更改为MultiDataTrigger,我会将你的答案标记为正确的。:)再次感谢!更新。谢谢事实上,我是根据你给我留下的关于我上一个答案的评论来思考整列和整行的。很高兴我能帮忙。