WPF AlternationIndex 用于控制 ControlTemplate 项的可见性

发布于 2024-11-04 19:15:35 字数 1221 浏览 0 评论 0原文

我正在为给定列表框的列表框项目使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木槿暧夏七纪年 2024-11-11 19:15:35

也许您忘记设置AlternationCount?无论如何,这是一个基于您的代码的小型独立工作示例:

<Grid>
    <Grid.Resources>
        <PointCollection x:Key="sampleData">
            <Point>1,2</Point>
            <Point>3,4</Point>
            <Point>5,6</Point>
        </PointCollection>
    </Grid.Resources>
    <ListBox ItemsSource="{StaticResource sampleData}" AlternationCount="2">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid Height="84" Width="700">
                                <Rectangle x:Name="_listItemBg" Width="700" Height="83" Fill="Red" Opacity="0.12"/>
                            </Grid>
                            <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>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>

Perhaps you forgot to set AlternationCount? In any case, here is a small self-contained working sample based on your code:

<Grid>
    <Grid.Resources>
        <PointCollection x:Key="sampleData">
            <Point>1,2</Point>
            <Point>3,4</Point>
            <Point>5,6</Point>
        </PointCollection>
    </Grid.Resources>
    <ListBox ItemsSource="{StaticResource sampleData}" AlternationCount="2">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid Height="84" Width="700">
                                <Rectangle x:Name="_listItemBg" Width="700" Height="83" Fill="Red" Opacity="0.12"/>
                            </Grid>
                            <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>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文