0

我有代码:

<ListBox Style="{StaticResource DeviceListBox}"
                 ItemsSource="{Binding MeterList, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                 SelectedItem="{Binding CurrentMeter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                 ItemContainerStyleSelector="{StaticResource DeviceListItemStyleSelector}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Style="{StaticResource DeviceListText}" Text="{Binding Name}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>

ItemContainerStyleSelector="{StaticResource DeviceListItemStyleSelector}"用来更改每个列表框项目的背景颜色(例如黑色或银色,请参阅 - http://msdn.microsoft.com/en-us/library/system.windows.controls.styleselector.aspx)。它有效。但是,如果我添加ItemContainerStyle="{StaticResource DeviceListItemStyle}"以创建一些触发器等,DeviceListItemStyleDeviceListItemStyleSelector不起作用。请帮帮我!)

4

1 回答 1

6

基于ItemContainerStyleSelector某种逻辑选择样式,因此显然手动设置样式将覆盖您的选择器应用的任何样式。

为什么不直接设置背景颜色ItemContainerStyle

<Style x:Key="DeviceListItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Background" Value="Black" />
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsSilver}" Value="True">
            <Setter Property="Background" Value="Silver" />
        </DataTrigger>
    </Style.Triggers>
</Style>
于 2011-12-15T15:27:49.920 回答