Wpf 为什么可以';我不能在边界上使用交替计数吗?

Wpf 为什么可以';我不能在边界上使用交替计数吗?,wpf,xaml,Wpf,Xaml,在下面的代码中,如果我更改 <Style TargetType="{x:Type Border}"> 到 它将为我的整个listboxitem添加正确的颜色。但我只想进入边境背景 <ListBox x:Name="FilteredMessagesListBox" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignme

在下面的代码中,如果我更改

  <Style  TargetType="{x:Type Border}">


它将为我的整个listboxitem添加正确的颜色。但我只想进入边境背景

  <ListBox  x:Name="FilteredMessagesListBox" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch"  SelectionMode="Extended" Background="Transparent" AlternationCount="2">
                        <ListBox.Resources>
                            <Style  TargetType="{x:Type Border}">
                                <Style.Triggers>
                                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                        <Setter Property="Background" Value="LightBlue"></Setter>
                                    </Trigger>
                                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                        <Setter Property="Background" Value="LightGreen"></Setter>
                                    </Trigger>
                                </Style.Triggers>
                            </Style>

                        </ListBox.Resources>

                        <ListBox.ItemTemplate >
                            <DataTemplate>
                                <DockPanel Margin="0,0,0,3">
                                    <Button x:Name="AttachmentImageButton" Click="AttachmentImageButton_Click" DockPanel.Dock="Bottom" MaxWidth="200" MaxHeight="200" HorizontalContentAlignment="Center" Visibility="{Binding ElementName=AttachmentImageButton, Converter={StaticResource cImageAttachmentToVisible}}" >
                                        <Image Source="{Binding Path=Attachment}" x:Name="AttachmentImage" />
                                    </Button>
                                    <Button x:Name="AttachmentButton" Click="AttachmentButton_Click" DockPanel.Dock="Right" Visibility="{Binding ElementName=AttachmentButton, Converter={StaticResource cAttachmentToVisible}}" >
                                        <Image Source="/MobilWPF;component/Resources/Images/PaperClip/PaperClip.jpg" Width="20" Height="20"/>
                                    </Button>

                                    <TextBlock Text="{Binding Converter={StaticResource cGetInstantMessageHeader}}" Width="120" TextWrapping="Wrap" HorizontalAlignment="Left"  Background="Transparent" FontSize="10" DockPanel.Dock="Left"/>

                                    <Border DockPanel.Dock="Left"  HorizontalAlignment="Stretch" CornerRadius="5">
                                        <DockPanel>
                                            <TextBlock Margin="5,0,0,0" Text="{Binding Path=Message}" TextWrapping="Wrap" DockPanel.Dock="Left" HorizontalAlignment="Left" Background="Transparent" FontSize="12"/>
                                        </DockPanel>
                                    </Border>
                                </DockPanel>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>

我试过这个:

  <Border DockPanel.Dock="Left"  HorizontalAlignment="Stretch" CornerRadius="5">
                                        <Border.Triggers>
                                            <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
                                                <Setter Property="Background" Value="LightBlue"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="1">
                                                <Setter Property="Background" Value="LightGreen"/>
                                            </DataTrigger>
                                        </Border.Triggers>
  <DockPanel>
                                            <TextBlock Margin="5,0,0,0" Text="{Binding Path=Message}" TextWrapping="Wrap" DockPanel.Dock="Left" HorizontalAlignment="Left" Background="Transparent" FontSize="12"/>
                                        </DockPanel>
                                    </Border>


在类型“ContentPresenter”上找不到静态成员“BackgroundProperty”

问题在于
AlternationIndex
是在容器上设置的(在本例中为
ListBoxItem
s),而不是在容器中的某些子容器上。您可以使用绑定到父级
ListBoxItem
上的
AlternationalIndex
DataTrigger
来解决此问题:

<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
    <Setter Property="Background" Value="LightBlue"/>
</DataTrigger>

谢谢@Kent Boogaart,你知道如何解决这个问题吗。。。在类型“ContentPresenter”上找不到静态成员“BackgroundProperty”
<DataTrigger Binding="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" Value="0">
    <Setter Property="Background" Value="LightBlue"/>
</DataTrigger>