WPF AlternationIndex 用于控制 ControlTemplate 项的可见性
我正在为给定列表框的列表框项目使用 ControlTemplate。 ControlTemplate 在 Style 中定义,并包含一个 Rectangle,其可见性需要根据 AlternationIndex 进行切换。尽管我了解如何使用 AlternationIndex 直接控制 ListBoxItem 的背景,但我不确定如何使用触发器来引用控件模板中的命名项。任何输入表示赞赏:
XAML摘录:
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Height="84" Width="700">
<!--
TURN ME ON FOR EVERY EVEN NUMBERED LIST ITEM
-->
<Rectangle x:Name="_listItemBg" Width="700" Height="83" Opacity="0.12">
...
我已尝试以下方法,但无济于事。正确的 XAML 语法让我无法理解:
<ControlTemplate.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Rectangle.Visibility" TargetName="_listItemBg" Value="Hidden" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Rectangle.Visibility" TargetName="_listItemBg" Value="Visible" />
</Trigger>
...
I am using a ControlTemplate for my ListBoxItems for a given ListBox. The ControlTemplate is defined in a Style and contains a Rectangle whose Visibility needs to be toggled based on the AlternationIndex. Although I see how to use AlternationIndex to control the background of the ListBoxItem directly, I'm not sure how I use the trigger to reference a named item in my control template. Any input is appreciated:
XAML Excerpt:
<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid Height="84" Width="700">
<!--
TURN ME ON FOR EVERY EVEN NUMBERED LIST ITEM
-->
<Rectangle x:Name="_listItemBg" Width="700" Height="83" Opacity="0.12">
...
I have tried the following, but to no avail. The correct XAML syntax evades me:
<ControlTemplate.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Rectangle.Visibility" TargetName="_listItemBg" Value="Hidden" />
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Rectangle.Visibility" TargetName="_listItemBg" Value="Visible" />
</Trigger>
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您忘记设置
AlternationCount
?无论如何,这是一个基于您的代码的小型独立工作示例:Perhaps you forgot to set
AlternationCount
? In any case, here is a small self-contained working sample based on your code: